1. 程式人生 > 實用技巧 >Codeforces 1325D Ehab the Xorcist(構造+異或)

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; }