## SGU 126 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=126

## 126. Boxes

time limit per test: 0.50 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.

`2 6`

### Sample Output

`2`

```a->a/gcd(a,b) b->b/gcd(a,b) if a+b=2^k then print k else print -1```

### CODE

```#include<stdio.h>   int gcd(int a,int b) { int r; while(b){ r=a%b; a=b; b=r; } return a; }   int main() { int a,b,c,k; scanf("%d%d",&a,&b); c=gcd(a,b); a/=c;b/=c; a+=b; b=1;k=0; while(b<a){ b*=2; k++; if(b==a){ printf("%dn",k); break; }else if(b>a||b<0){ printf("-1n"); return 0; } } return 0; }```
» 本博客采用署名 2.5 中国大陆许可协议进行许可，本文版权归作者所有，欢迎转载，但必须在明显位置给出原文连接。
anyShare分享到：