CodeChef div4 (H) The XOR-OR Dilemma

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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值