<传送门>

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

【题目大意】

简单地说就是:给你两个数a和b,现在你可以“将大数-小数,小数变为原来2倍”,问你能否在有限次的操作后使得其中哟个数等于0,另外一个数为原来两个数的和。

若可能,输出步数;不可能输出-1.

【题目分析】

一开始的时候,我用每次都使得a为大数,b为小数,但是这样会出现循环,因为每次都维护他们的前后大小,势必会造成循环。

如果我们只是一开始维护一次大小关系,以后就不管他了,这样当b增加到>=a的时候,还不满足一个为0的条件,那么就说明不可能实现这样的操作。

证明过程如下:

give two numbers: a and b;(a!=b)

a=max(a,b);    b=min(a,b);

重复:  a=a-b;  b=b+b;    当b大于等于a时,以后的都是重复开始的那两个数字,所以前面不能实现题目的操作的话,后面不可能实现。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int a,b; int sovle()
{
if(a == b) return 1;
else if(a == 0 || b == 0) return 0;
int cnt = 1;
int tmp;
if(a < b)
{
tmp = a;
a = 2*tmp;
b = b - tmp;
}
else if(a > b)
{
tmp = b;
b = 2*tmp;
a = a - tmp;
} if(a > b)
{
tmp = a;
a = b;
b = tmp;
}
if(b%a != 0) return -1;
else{
while(a < b)
{
tmp = a;
a = tmp + tmp;
b = b - tmp;
cnt ++;
if(a == b)
break;
}
if(a == b) return cnt + 1;
else return -1;
}
} int main()
{
while(scanf("%d%d",&a,&b) != EOF)
{
int ans = sovle();
printf("%d\n",ans);
}
return 0;
}

  

SGU 126. Boxes --- 模拟的更多相关文章

  1. SGU 126 Boxes(模拟题|二进制)

    Translate:Sgu/126 126. 盒子 time limit per test: 0.5 sec. memory limit per test: 4096 KB 有两个盒子. 第一个盒子里 ...

  2. 找规律 SGU 126 Boxes

    题目地址:http://acm.sgu.ru/problem.php?contest=0&problem=126 /* 找规律,智商不够,看了题解 详细解释:http://blog.csdn. ...

  3. sgu 126 Boxes

    题意:较大的容量减较小的容量,较小的容量翻倍.问操作几回其中一个空. 开始用set判重,重复就不可行.不过状态最多有2e18种.不仅爆内存,还超时.然后找规律.发现只有比例为1:1,1:3,1:7,3 ...

  4. Boxes - SGU 126(找规律)

    题目大意:有两个箱子,一个箱子装了A个球,一个箱子装了B个球,可以从球多的那个箱子拿出来球少的箱子里面球的总数放在少的那个箱子里面,问能否把球全部放在一个箱子里面? 分析:很容易求出来最后放的拿一下一 ...

  5. Codeforces Round #158 (Div. 2) C. Balls and Boxes 模拟

    C. Balls and Boxes time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. SGU 191.Exhibition(模拟)

    时间限制:0.25s 空间限制:4M 题意: 有两个公司A.B,他们要展览物品,但是A公司的展柜要放B公司的物品,B公司的展柜要放A公司物品.最开始只有一个空柜台,从指定的一个公司开始,轮流进行操作, ...

  7. Codeforces 821C Okabe and Boxes(模拟)

    题目大意:给你编号为1-n的箱子,放的顺序不定,有n条add指令将箱子放入栈中,有n条remove指令将箱子移除栈,移出去的顺序是从1-n的,至少需要对箱子重新排序几次. 解题思路:可以通过把栈清空表 ...

  8. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  9. 今日SGU 5.4

    SGU 127 题意:给你n个数字,和m,k,问你有多少个数字的m次幂可以被k整除 收获:快速幂 #include<bits/stdc++.h> #define de(x) cout< ...

随机推荐

  1. 接口调试工具Postman之自动同步Chrome cookies,实现自动登陆验证

    前言 在前后端分离开发时,做为后端开发人员,要求独立开发完成某个接口后,开发人员自己需要先测试通过后再提交给测试人员进行测试,否则会出现到测试人员哪里业务流程根本就走不通,或者BUG会过多的情况等. ...

  2. memoryDiary

    What did you accomplish today? , did you exercise today? Do you care about the people around you tod ...

  3. [RN] React Native 再按一次退出

    实现 React Native 再按一次退出 单页面: ... componentWillMount() { BackHandler.addEventListener('hardwareBackPre ...

  4. [PHP] Laravel 5.5 的 BCrypt对密码进行加密及密码验证

    Laravel 5.5 的 BCrypt对密码进行加密及密码验证 一.加密 方法1) $password= Hash::make('密码'); 方法2) /也可直接使用 bcrypt 的 functi ...

  5. git 提交代码报错failed to push some refs to 解决笔记

    Administrator@SC- MINGW64 /e/gitrepository (master) $ git push django master To github.com:zgc137/dj ...

  6. 高考数学答卷策略[K12论坛转载]

    一.试卷上给你的启发 1.试卷上有参考公式,80%是有用的,它为你的解题指引了方向: 2.解答题的各小问之间有一种阶梯关系,通常后面的问要使用前问的结论.如果前问是证明,即使不会证明结论,该结论在后问 ...

  7. linux修改/etc/profile权限

    修改/etc/profile时提示为只读文件,不允许修改, 敲#chmod 777 /etc/profile后仍不允许修改 解决办法: 在root权限下敲 #:mount -o remount,rw ...

  8. 使用List中remove方法时需要注意的问题

    String str1 = new String("1"); String str2 = new String("2"); String str3 = new ...

  9. CAP原则 (阿里)

    CAP原则又称CAP定理,指的是在一个分布式系统中,一致性(Consistency).可用性(Availability).分区容错性(Partition tolerance).CAP 原则指的是,这三 ...

  10. centos7安装hadoop2.7.7

    下载hadoop-2.7.7 网址如下 https://www-eu.apache.org/dist/hadoop/core/ 移动到/opt 路径下 在/opt下新建一个文件夹,名为app mkdi ...