7.13
CodeChef div4 (H) The XOR-OR Dilemma
Chef has an array AA of length NN such that Ai=iAi=i.
In one operation, Chef can pick any two elements of the array, delete them from AA, and append either their bitwise XOR or their bitwise OR to AA.
Note that after each operation, the length of the array decreases by 11.
Let FF be the final number obtained after N−1N−1 operations are made. You are given an integer XX, determine if you can get F=XF=X via some sequence of operations.
In case it is possible to get F=XF=X, print the operations too (see the section Output format for more details), otherwise print −1−1.
给定1~n,你可以进行两种操作:
- 1 x y if you want replace x and y with x OR y.
- 2 x y if you want replace x and y with x XOR y
然后将x,y移除并将新值插入。给定x,求使得最终剩下的值为x的方案
构造的思路就是把所有的数全部或起来,然后和最终的数做异或得到tmp。然后用二进制拼接出tmp,用或操作,然后把剩下的没用到的数或起来最终两数异或便是答案。期间,特判不存在的解。
const int N=200010,M=N*2,mod=1e9+7;
int n,m,k,a[N];
struct Op{
int op,a,b;
};
void solve(){
n=read(),m=read();
int all=0;rep(i,1,n) all|=i;
if(all<m){
print(-1);
return ;
}
int tmp=(all^m);
if(tmp==0){
int last=1;
for(int i=2;i<=n;++i){
printf("1 %d %d\n",i,last);
last|=i;
}
return ;
}
vector<Op> vec(0);
vector<int> temp;
map<int,int> mp;
for(int i=0;(1<<i)<=tmp;++i)
if(tmp>>i&1){
mp[1ll<<i]=1;
temp.push_back(1ll<<i);
}
int cnt=0,last=0;
for(int i=1;i<=n;++i){
if(mp.count(i)) continue;
if(cnt==0) {
cnt++;
last=i;
}
else {
vec.push_back({1,last,i});
last|=i;
}
}
if(last!=all){
print(-1);
return ;
}
int u=0;
for(int i=0;i<temp.size();++i){
if(i==0) u=temp[i];
else vec.push_back({1,u,temp[i]}),u|=temp[i];
}
vec.push_back({2,last, u});
for(auto u:vec){
printf("%d %d %d\n",u.op,u.a,u.b);
}
}