链接:https://codeforces.ml/contest/1325/problem/D
Given 2 integers uu and vv, find the shortest array such that bitwise-xor of its elements is uu, and the sum of its elements is vv.
Input
The only line contains 2 integers uu and vv (0≤u,v≤1018)(0≤u,v≤1018).
Output
If there's no array that satisfies the condition, print "-1". Otherwise:
The first line should contain one integer, nn, representing the length of the desired array. The next line should contain nn positive integers, the array itself. If there are multiple possible answers, print any.
Examples
input
Copy
2 4
output
Copy
2 3 1
input
Copy
1 3
output
Copy
3 1 1 1
input
Copy
8 5
output
Copy
-1
input
Copy
0 0
output
Copy
0
Note
In the first sample, 3⊕1=23⊕1=2 and 3+1=43+1=4. There is no valid array of smaller length.
Notice that in the fourth sample the array is empty.
代码:
#include<bits/stdc++.h>
using namespace std;
long long s,n,h,k,l,r,x,u,v,max1=0;
map<long long,long long>m,q;
long long a,b,c;
int main()
{
cin>>u>>v;
if(u==0&&v==0)
{
cout<<0;
return 0;
}
int i=0;
k=u;
while(k)
{
if(k%2==1)
m[i]=1;
else
m[i]=0;
i++;
k/=2;
}
a=0;
for(int j=0;j<=i;j++)
{
if(m[j]==1)
{
n=pow(2,j);
a+=n;
v-=n;
}
}
if(v<0)
cout<<-1;
else
{
if(v==0)
{
cout<<1<<endl;
cout<<a;
}
else
{
k=v;
int p=0;
while(k)
{
if(k%2==1)
q[p]=1;
else
q[p]=0;
p++;
k/=2;
}
b=0;
c=0;
for(int j=1;j<=p;j++)
{
if(q[j]==1&&m[j-1]==0)
{
n=pow(2,j-1);
b+=n;
a+=n;
v-=n*2;
}
else if(q[j]==1)
{
n=pow(2,j-1);
b+=n;
c+=n;
v-=n*2;
}
}
if(q[0]==1)
cout<<-1;
else
{
if(c!=0)
{
cout<<3<<endl;
cout<<a<<" "<<b<<" "<<c;
}
else
{
cout<<2<<endl;
cout<<a<<" "<<b;
}
}
}
}
}

本文探讨了一个算法问题,即寻找满足特定位运算和总和条件的最短整数序列。输入为两个整数u和v,目标是找到一个序列,其元素的按位异或等于u,元素总和等于v。文章通过分析输入数据的二进制表示,提出了一种有效的解决方案,并附带了完整的C++实现代码。
404

被折叠的 条评论
为什么被折叠?



