ICPC(2020)济南站 G. Xor Transformation 题解

96 阅读1分钟

题目链接

思路

比赛时思路是把xx分两步变成yy,第一步是把xx二进制中与yy重合的部分异或成和yy一模一样的,第二步是把多出yy的那一部分全部异或成00,这样就能保证每一次异或的都比当前xx小。
之后看到题解,做法更简单。首先y0y\neq 0,因此xyxx\oplus y\neq x
如果xy<xx\oplus y<x,答案就是xyx\oplus y
否则只存在xy>xx\oplus y>x,根据异或运算的自反性xyx=yx\oplus y\oplus x=y,答案就是xyx\oplus yxx

异或运算的性质

  • 交换律
  • 结合律
  • 自反性(xyx=yx\oplus y\oplus x=y)因此可以用异或来交换两个数的值:
x=xy=x1y原先的x称之为x1y=xy=x1yy=x1x=xy=x1yx1=yx=x\oplus y=x_1\oplus y\Rightarrow 原先的x称之为x_1\\ y=x\oplus y=x_1 \oplus y\oplus y=x_1\\ x=x\oplus y=x_1\oplus y\oplus x_1=y
  • xx=0x0=xx\oplus x=0,x\oplus 0=x
  • xy=zx\oplus y=z,则xz=yx\oplus z=y
  • x1=xx\oplus-1=\sim x

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll x,y;
int main()
{
    cin>>x>>y;
    if((x^y)<x)
    {
        cout<<1<<endl;
        cout<<(x^y)<<endl;
        return 0;
    }
    cout<<2<<endl;
    cout<<y<<" "<<x<<endl;
}