博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #628D
阅读量:3952 次
发布时间:2019-05-24

本文共 1787 字,大约阅读时间需要 5 分钟。

D. Ehab the Xorcist

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Given 2 integers uu and vv, find the shortest array such that  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

23 1

input

Copy

1 3

output

Copy

31 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.

给你一个u和v,然后让你构造一个元素个数最小的数组,使得这些数字的异或和为u,前缀和为v。

异或又被称为不进位加法,,

a+b=a$\oplus$b+2*a&b;

这个式子可以这样理解,a$\oplus$b 只是计算出每一位不考虑进位时候的情况,可以+还是进位的,进位的时候肯定是a,b 当前位都是1,才会往前进位。而a&b 第i为1,说明第i位该往前进位,而乘2则是整体左移,就是往前进位。然后在加上刚才没考虑进位的情况。

这个题目不存在解的情况,就是u>v 的情况和u,v奇偶性不同的情况。前者是因为异或是不进位加法,所以肯定不会大于加法的值;后者是因为,如果某一位u,v不一致,则肯定是进位造成的,而最后一位肯定没有进位,所以要为1都为1,要为0,都为0。
对于u==v 的情况直接输出u 就行。

(这个大佬的思路太通俗易懂了,,很感谢)。。。

对于一般情况,

a⊕b=u

a+b=a⊕b+2⋅a&b =u+2.a&b==v

因为u\oplus x \oplus x=u

所以
我们令x=(v-u)/2 就行
答案就是[u,x,x] 
第一个样例长度为2
是因为u⊕x=u+x 
所以是[u+x,x]

#include 
using namespace std;typedef long long ll;int main(){ ios::sync_with_stdio(false); ll u,v; cin>>u>>v; if(u==v&&u==0) { puts("0"); return 0; } else if(u==v) { cout<<1<<" "<
<
v||((u&1)!=(v&1))) { puts("-1"); return 0; } ll x=(v-u)/2; if((u^x)==(u+x)) { cout<<2<
<
<<" "<
<

 

转载地址:http://ltyzi.baihongyu.com/

你可能感兴趣的文章
6个好习惯让你做个优秀的开发者
查看>>
platform_device的注册过程分析
查看>>
linux2.6中的工作队列接口 workqueue_struct
查看>>
等待队列学习笔记
查看>>
MTK G-sensor
查看>>
linux工作队列
查看>>
Linux工作队列的使用
查看>>
linux kernel 工作队列
查看>>
移植Android 到mini2440
查看>>
Linux 进程调度原理
查看>>
globalfifo精彩问答
查看>>
ARM 启动过程
查看>>
ARM开发总结的小知识 Code,RO-data,RW-data,ZI-
查看>>
Linux驱动程序开发 - 设备驱动模型初探
查看>>
创业必看!
查看>>
Linux墙上时间
查看>>
怎样写 Linux LCD 驱动程序
查看>>
PADS Logic图文教程:更改切换元件
查看>>
PADS Logic图文教程:更改切换元件
查看>>
全面的framebuffer详解
查看>>