There are two boxes.
Time Limit:500MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u
Description
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
AC代码:
#include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> using namespace std; bool Is2n(int n) { if (n<=0) return false; if ((n & ((~n)+1))==n) return true; else return false; } int main() { long long a; long long b; cin>>a>>b; int count=0; int result=-1; if(a>b) { long long temp1=0; temp1=a; a=b; b=temp1; } if(a==0) { cout<<count; } else { int sum=a+b; int m=b; int n=a; int r=0; do{ r=m%n; m=n; n=r; }while(r!=0); int temp=sum/m; if(Is2n(temp)) { while(temp>1) { temp=temp/2; count++; } cout<<count; } else { cout<<result; } } }

浙公网安备 33010602011771号