-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathP126.java
66 lines (52 loc) · 1.41 KB
/
P126.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
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
*
*/
import java.util.*;
public class P126 {
public static int gcd(int a, int b) {
while(b != 0) {
int temp = a;
a = b;
b = temp % a;
}
return a;
}
public static int log2(int a) {
int res = 0;
while(a >= 2) {
res++;
a >>= 1;
}
return res;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int res = 0;
if(a != 0 && b != 0) {
int N = (a + b) / gcd(a, b);
int n = log2(N);
res = 1 << n == N ? n : -1;
}
System.out.println(res);
}
}