Codeforces Round #630 (Div. 2) D Walk on Matrix

題意

給你一個kk,讓你構造一個矩陣nm(n,m<=500)n*m,(n,m<=500)
方式一dp[i][j]=max(dp[i1][j]dp[i][j] = max(dp[i-1][j] & dp[i][j],dp[i][j1]dp[i][j], dp[i][j-1] & dp[i][j])dp[i][j])(1,1)>(1,n)dp[n][n](1,1)-->(1,n)的最大值,即dp[n][n]
方式二,找一條路(1,1)–>(1,n)的最大值(路上的值&起來)
方式二 減 方式一 = k
構造出這樣的矩陣

思路

這樣dp爲什麼比實際最優要小。
舉個栗子:
7 3 0
4 7 3
dp[2][2]=4dp[2][2] = 4然後轉移到dp[2][3]dp[2][3]的時候,dp[2][3]=0dp[2][3] = 0
但實際上是有一條從(1,1)>(1,2)>(2,2)>(2,3)(1,1)-->(1,2)-->(2,2)-->(2,3)等於3。

然後我們就以這種方式來構造,矩陣大小就是2*3,然後(1,1)(2,2)(1,1) 和 (2,2)位置上的數的二進制表示都是1(11111(2)_{(2)}),(1,3)位置爲0,保證(3,3)只從(2,2)更新過來,而(1,1)(2,2)(1,1) 和 (2,2)這麼取值的作用就是不改變(1,2和(2,1)的值。
這樣的一個矩陣,dp求的值是a&b,但是實際答案是b(與例子一樣), 很顯然a比b大,然後我們又要讓 b - a&b = k; 可以使a&b = 0,b = k,就構造出解,a的取值要比k大,且k二進制位上有1的a不能有.
在這裏插入圖片描述

code

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
const int man = 2e5+10;
#define IOS ios::sync_with_stdio(0)
template <typename T>
inline T read(){T sum=0,fl=1;int ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')fl=-1;
for(;isdigit(ch);ch=getchar())sum=sum*10+ch-'0';
return sum*fl;}
template <typename T>
inline void write(T x) {static int sta[35];int top=0;
do{sta[top++]= x % 10, x /= 10;}while(x);
while (top) putchar(sta[--top] + 48);}
template<typename T>T gcd(T a, T b) {return b==0?a:gcd(b, a%b);}
template<typename T>T exgcd(T a,T b,T &g,T &x,T &y){if(!b){g = a,x = 1,y = 0;}
else {exgcd(b,a%b,g,y,x);y -= x*(a/b);}}
#ifndef ONLINE_JUDGE
#define debug(fmt, ...) {printf("debug ");printf(fmt,##__VA_ARGS__);puts("");}
#else
#define debug(fmt, ...)
#endif
typedef long long ll;
const ll mod = 1e9+7;

int main() {
    #ifndef ONLINE_JUDGE
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt","w",stdout);
    #endif
    int k;
    cin >> k;
    cout << 2 << " " << 3 << endl;
    cout << ((1ll<<18)-1) << " " << k << " " << 0 << endl;
    cout << (1ll<<17) << " " << ((1ll<<18)-1) << " " << k << endl;
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章