https://codeforces.com/problemset/problem/1332/D
思路:
首先要知道这个dp为啥是错的。
如果dp是转移和的话,那是对的。但是这个是错的,因为位运算不能保证之前的最大转移到这里就是最大。
观察一下这个dp实际上是怎么贪心转移的。
对于某处的(i,j)其转移是从上面和左边转移过来的,取其位运算大者。而二进制的性质就是高位的1大于其低位所有1之和。所以这个dp实际上是按照二进制的最高位的1来贪心的。
知道了这一点后。我们就可以尝试构造了。
往特殊的去构造,让该算法的答案为0,而正确答案是k。
二进制全1 最高位是1 0
k的二进制 二进制全1 k的二进制
举例
111 100 0
011 111 011(k)
如此构造,其贪心算法会在(2,3)处取其最高位的1。再和k的二进制进行结合就是0,然正解就是k
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=70;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn][maxn];
LL p[maxn];
int main(void)
{
cin.tie(0);std::ios::sync_with_stdio(false);
p[0]=1;
for(LL i=1;i<maxn;i++) p[i]=p[i-1]*2;
LL k;cin>>k;
if(k==0){
cout<<1<<" "<<1<<"\n";
cout<<1<<"\n";
}
else{
cout<<2<<" "<<3<<"\n";
LL sum=0;
for(LL i=0;i<=17;i++) sum+=p[i];
a[1][1]=sum;
a[1][2]=p[17];
a[1][3]=0;
a[2][1]=k;
a[2][2]=sum;
a[2][3]=k;
for(LL i=1;i<=2;i++){
for(LL j=1;j<=3;j++){
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
}
return 0;
}