D. Ehab the Xorcist(异或和关系)

题意:求最短的一组数,使这些数异或为u,和为v。

解法:1、可知奇偶性对于异或和加法来说是等价的,所以u、v奇偶性相同才满足条件。2、一个数拆成几个数异或一定不大于原数,所以u <= v。

3、当u==v&&u==0,为0,4、u==v&&u != 0,等于u

5、u ^ 0 = 0 , 考虑拆成3个数,u , (v-u)/2, (v-u)/2.满足。

6、最短,根据a ^ b = a + b - 2 * (a & b).  假设答案为a ,b . a ^ b = (u ^ x) ^ x , a + b = (u + x) + x .如果u^x = u + x .则可以合并。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
#define int ll
#define mod 998244353
#define gcd(m,n) __gcd(m, n)
#define rep(i , j , n) for(int i = j ; i <= n ; i++)
#define red(i , n , j)  for(int i = n ; i >= j ; i--)
#define ME(x , y) memset(x , y , sizeof(x))
int lcm(int a , int b){return a*b/gcd(a,b);}
ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;}
//int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;}
//const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len}
#define INF  0x3f3f3f3f
#define PI acos(-1)
#define pii pair<int,int>
#define fi first
#define se second
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define size(v) (int)(v.size())
#define cin(x) scanf("%lld" , &x);
const int N = 1e7+9;
const int maxn = 2e5+9;
const double esp = 1e-6;
 
 
void solve(){
    int u , v ;
    cin >> u >> v ;
    if(u > v || u % 2 != v % 2){
        cout << -1 << endl;
        return;
    }
    if(u == v && v == 0){
        cout << u << endl;
        return;
    }
    if(u == v && v != 0){
        cout << 1 << endl << u << endl;
        return ;
    }
    int a = (v - u) / 2 ;
    if((a ^ u) == a + u){
        cout << 2 << endl;
        cout << (u + v) / 2 << " " << a << endl;
    }else{
        cout << 3 << endl;
        cout << u << " " << a << " " << a << endl;
    }
}
 
signed main()
{
    //ios::sync_with_stdio(false);
    int t ;
    //scanf("%lld" , &t);
    //while(t--)
        solve();
}

  

 

posted @ 2020-03-21 06:02  无名菜鸟1  阅读(340)  评论(0编辑  收藏  举报