126. Boxes

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

There are two boxes. There are A balls in the first box, and B balls in the second box (0 < A + B < 2147483648). It is possible to move balls from one box to another. From one box into another one should move as many balls as the other box already contains. You have to determine, whether it is possible to move all balls into one box.

Input

The first line contains two integers A and B, delimited by space.

Output

First line should contain the number N - the number of moves which are required to move all balls into one box, or -1 if it is impossible.

Sample Input

Sample Output

2 6

Sample Output

2

思路 1 只记录两个箱子的差值X,那么X=0的时候就能1次成功了
2 X的转移是X=SUM-2*X或者X=2*X-SUM(也即绝对值,X>0),注意到每次分母扩2倍,也就是说不超过32次
之后随便写一写就好啦,这个纯属胡整,我要睡觉!
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
int a,b,sum;
queue<int >que;
set <int >st;
int main(){
scanf("%d%d",&a,&b);
if(a>b)swap(a,b);
if(a==0||b==0)puts("0");
else if(a==b)puts("1");
else if((b-a)*2==a+b)puts("2");
else if((b+a)&1)puts("-1");
else {
int sub=b-a;
sum=a+b;
st.insert(sub);
que.push(sub);
int step=0;
while(!que.empty()){
sub=que.front();que.pop();step++;
if(sub==0||step>64)break;
int t=abs(sum-2*sub);
if(st.count(t)==0){que.push(t);st.insert(t);}
}
if(sub==0)printf("%d\n",step);
else puts("-1");
}
return 0;
}

  

快速切题sgu126. Boxes的更多相关文章

  1. 快速切题sgu127. Telephone directory

    127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...

  2. 快速切题 sgu123. The sum

    123. The sum time limit per test: 0.25 sec. memory limit per test: 4096 KB The Fibonacci sequence of ...

  3. 快速切题 sgu120. Archipelago 计算几何

    120. Archipelago time limit per test: 0.25 sec. memory limit per test: 4096 KB Archipelago Ber-Islan ...

  4. 快速切题 sgu119. Magic Pairs

    119. Magic Pairs time limit per test: 0.5 sec. memory limit per test: 4096 KB “Prove that for any in ...

  5. 快速切题 sgu118. Digital Root 秦九韶公式

    118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of ...

  6. 快速切题 sgu117. Counting 分解质因数

    117. Counting time limit per test: 0.25 sec. memory limit per test: 4096 KB Find amount of numbers f ...

  7. 快速切题 sgu116. Index of super-prime bfs+树思想

    116. Index of super-prime time limit per test: 0.25 sec. memory limit per test: 4096 KB Let P1, P2, ...

  8. 快速切题 sgu115. Calendar 模拟 难度:0

    115. Calendar time limit per test: 0.25 sec. memory limit per test: 4096 KB First year of new millen ...

  9. 快速切题 sgu113 Nearly prime numbers 难度:0

    113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...

随机推荐

  1. UVa 11082 Matrix Decompressing - 网络流

    开始眨眼一看怎么也不像是网络流的一道题,再怎么看也觉得像是搜索.不过虽然这道题数据范围很小,但也不至于搜索也是可以随随便便就可以过的.(不过这道题应该是special judge,因为一题可以多解而且 ...

  2. Python3基础 str title 单词首字母大写,其余均为小写

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  3. Python3基础 str format 四舍六入五凑偶 保留一位小数

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  4. Python3基础 time.localtime 当前系统的年月日 时分秒

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  5. Python3基础 os chdir 改变工作目录

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. 获取转UTF8的字符串

    /// <summary> /// 获取转UTF8的字符串 /// </summary> /// <param name="strWord">& ...

  7. 1、Python快速入门(0529)

    学习来自马哥教育的视频,感谢马哥 编程语言: 用户: 问题空间 计算机:解决问题 解空间 抽象: 机器代码-->微码编程-->高级语言 (语言的高下级的是根据语言是否被人类容易理解或者更接 ...

  8. Python matplot画散列图

    同matlab一样,matplot也可画散列图scatter. import numpy as np import matplotlib.pyplot as plt #fig = plt.figure ...

  9. 安装cartographer_ros

    这里使用的是hitcm(张明明)的github地址,由于google官方的教程需要FQ下载一些文件,因此容易失败,经验证hitcm(张明明)对原文件进行了少许修改后可以成功安装,在他的修改中核心代码不 ...

  10. Qt5_qt.conf

    本站所有文章由本站和原作者保留一切权力,仅在保留本版权信息.原文链接.原 文作者的情况下允许转载,转载请勿删改原文内容, 并不得用于商业用途. 谢谢合作. 原文链接:用qt.conf和qtconfig ...