-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbomb_baby.java
29 lines (26 loc) · 1.06 KB
/
bomb_baby.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
package com.google.challenges;
import java.math.BigInteger;
public class Answer {
// alternatives: AtomicBoolean, Pair class
static private boolean isPossible;
/*
one is divisible by another and bigger than 1 => impossible
recursive solution is similar to implementation of gcd
Formula: F(a, b) = (a / b) + F(b, a mod b)
*/
public static BigInteger cycles(BigInteger a, BigInteger b) {
if (b.compareTo(BigInteger.valueOf(0)) == 0) return a;
else if (a.mod(b) == BigInteger.valueOf(0) && b.compareTo(BigInteger.valueOf(1)) > 0) isPossible = false;
return a.divide(b).add(cycles(b, a.mod(b)));
}
public static String answer(String M, String F) {
// Your code goes here.
BigInteger m = new BigInteger(M);
BigInteger f = new BigInteger(F);
isPossible = true;
// 2 is subtracted because we start from (0, 0)
BigInteger res = cycles(m, f).subtract(BigInteger.valueOf(2));
if (!isPossible) return "impossible";
else return res.toString();
}
}