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

  1. 2 6

Sample Output

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

  

快速切题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. IT人士级别的划分

    IT领袖:年入过亿(例如任正非.马化腾.李彦宏.丁磊.马云等,包括期权股票以及投资理财等收入.) IT大哥:年入千万(级别次于以上几位大佬的公司老板,不缺钱,普遍对上一条里的人物羡慕嫉妒恨.) IT精 ...

  2. HDU 1358 Period(KMP+最小循环节)题解

    思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...

  3. 【lrzsz 】Linux安装上传下载功能

    1.在线安装服务 # yum -y install lrzsz 2.上传 rz 3.下载 sz

  4. trigger自动执行事件

    html <button>点击文字变红</button> <p>trigger出发点击事件</p> js $('button').click(funct ...

  5. thymeleaf和easyui配合可能出现的错误

    thymeleaf和easyui 在easyui的内页,不再使用th:href引入静态资源文件. 在easyui页面中,script执行easyui自己的方法要加入: <script th:in ...

  6. linq——group by

    多列排序&&聚合函数 var result = from i in                (from uh in db.UserHistories                ...

  7. basename、dirname、alias、date

    basename 此命令用于打印目录或者文件的基本名称. basename和dirname命令通常用于shell脚本中的命令替换来指定和指定的输入文件名称有所差异的输出文件名称. basename ( ...

  8. JDK tools之jps和jstack诊断Java程序

    大部分Java开发者可能知道有这么个工具,但是没怎么用过,每次还得百度一下.我也是之一 -_-!!. 每次遇到

  9. MVC ---- T4(1)

    T4 模板编辑插件:tangibleT4EditorPlusModellingToolsVS2012.msi 下载地址:http://t4-editor.tangible-engineering.co ...

  10. C++:为什么unique_ptr的Deleter是模板类型参数,而shared_ptr的Deleter不是?

    为什么unique_ptr的Deleter是模板类型参数,而shared_ptr的Deleter不是? template <class T, class D = default_delete&l ...