-
Notifications
You must be signed in to change notification settings - Fork 5
/
MultiplyBy2DivideBy6.java
58 lines (56 loc) · 1.02 KB
/
MultiplyBy2DivideBy6.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
/*https://codeforces.com/problemset/problem/1374/B*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class MultiplyBy2DivideBy6
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int tests = Integer.parseInt(reader.readLine());
double n;
int steps;
boolean isPossible;
while (tests-- > 0)
{
n = Double.parseDouble(reader.readLine());
steps = 0;
isPossible = true;
if (n == 1)
{
System.out.println("0");
isPossible = false;
}
else if (n%3 != 0)
{
System.out.println("-1");
isPossible = false;
}
else
{
while (n != 1)
{
while (n%6 == 0)
{
++steps;
n /= 6;
}
if (n == 1)
{
break;
}
else if (n%3 != 0)
{
System.out.println("-1");
isPossible = false;
break;
}
n *= 2;
++steps;
}
}
if (isPossible)
System.out.println(steps);
}
}
}