126. Boxes time limit per test: 0.25 sec. 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 6
a b
先从b移2个给a,
a就有4个了,
a和b都有4个了,
再把a的4个移到b,
a就全部到b了,
这样就操作了俩步。。
分析
我们用(x, y)表示两个盒子里分别有x个球和y个球(不区分是哪个盒子)。假设总共有n个球,经过k步把所有球移到一个盒子里。如果反过来推的话,那么各盒子中球的个数为:
第k步: (0, n);
第k-1步:(n/2, n/2);
第k-2步:(n/4, 3n/4);
第k-3步:(n/8, 7n/8)或(3n/8, 5n/8);
第k-4步:(n/16, 15n/16)或(3n/16, 13n/16)或(5n/16, 11n/16)或(7n/16, 9n/16);
......
这样,我们就发现了规律:除(0, n)这种情况外(一步即可),若(A, B)能化成(x*c, y*c)这种形式,(c为A、B的最大公约数,x和y均为奇数且x+y=2^k,k=1,2,...)则k即为所求的最小步数,输出k;否则,输出-1。
2k=(x+y)/gcd(a,b),k即为答案
代码:
#include<iostream>
#include<cstdio>
using namespace std;
int a,b,t,gg,i;
int main()
{
int m,k;
while(~scanf("%d %d",&a,&b))
{
if(a>b)
swap(a,b);
t=b%a;
while(t!=0)
{
b=a;
a=t;
t=b%a;
}
gg=a;
a-=gg;
b-=gg;
m=a+b;
k=0;
while(m>1)
{
if(m%2==0)
{
k++;
m=m/2;
}
else
{
k=-1;
break;
}
}
printf("%d\n",k);
}
return 0;
}