Page MenuHomedesp's stash

Solution.java
No OneTemporary

Solution.java

import java.math.BigInteger;
public class Solution {
public static String solution(String x, String y) {
BigInteger m = new BigInteger(x), f = new BigInteger(y);
boolean found = false;
BigInteger count = BigInteger.ZERO;
while (!found && (m.signum() == 1 && f.signum() == 1)) { //loop when not found and values are not negative
int comp = m.compareTo(f);
if (comp > 0) {
BigInteger[] vals = f.equals(BigInteger.ONE) ? //dividing one is the only case where it will not be equivalent to multiple subtraction, thus we must make an exception
new BigInteger[] { BigInteger.ONE, m.subtract(f) } : m.divideAndRemainder(f);
count = count.add(vals[0]);
m = vals[1];
} else if (comp < 0) {
BigInteger[] vals = m.equals(BigInteger.ONE) ? //as above
new BigInteger[] { BigInteger.ONE, f.subtract(m) } : f.divideAndRemainder(m);
count = count.add(vals[0]);
f = vals[1];
} else {
if (m.equals(BigInteger.ONE)) { //no need to check f since they are equal
found = true;
} else {
throw new IllegalStateException(); //it should never be possible for two non-1 values to be equal under the bomb replication constraints
}
}
}
return found ? count.toString() : "impossible";
}
}

File Metadata

Mime Type
text/x-java
Expires
Mon, May 11, 1:28 PM (1 d, 17 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
ae/82/ccb9273de05aea6babd6de0f8e35

Event Timeline