Codeforces 1325D Ehab the Xorcist(构造+异或)
题意:给出u,v,分别表示数组a的异或和,和数组a的和。求构造出最短的数组a。u<1e18
题解:显然u>v || (u&1)!=(v&1)无解,u==v=0时,数组为空,u==v!=0时,数组长度为1,元素为u即可。开始考虑构造,0^u=u,可令x=(v-x)/2,则x x u 满足条件,当(x^u)==(v-x)时,可化简成x^u, x。
#include <bits/stdc++.h> #define IOS ios::sync_with_stdio(false);cin.tie(0) #define fre freopen("C:\\in.txt", "r", stdin) #define _for(i,a,b) for(int i=a; i< b; i++) #define _rep(i,a,b) for(int i=a; i<=b; i++) #define lowbit(a) ((a)&-(a)) #define inf 0x3f3f3f3f #define endl "\n" using namespace std; typedef long long ll; template <class T> void read(T &x) { char c; bool op=0; while(c=getchar(), c<'0'||c>'9') if(c=='-') op=1; x=c-'0'; while(c=getchar(), c>='0'&&c<='9') x=x*10+c-'0'; if(op) x=-x; } ll T, u, v; int main() { //fre; T=1; //read(T); while(T--) { read(u), read(v); ll x=(v-u)/2; if(u>v || (u&1)!=(v&1)) printf("-1\n"); else if(u==v && u==0) printf("0\n"); else if(u==v) printf("%d\n%lld", 1, u); else if((x^u)==(v-x)) printf("%d\n%lld %lld\n", 2, x^u, x); else printf("%d\n%lld %lld %lld\n", 3, x, x, u); } return 0; }