-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP113.java
57 lines (53 loc) · 1.4 KB
/
P113.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class P113 {
public static void main(String[] args) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
String line;
BigInteger TWO = BigInteger.ONE.add(BigInteger.ONE);
int totalIterations = 0;
while((line = rd.readLine()) != null) {
int n = Integer.parseInt(line);
BigInteger p = new BigInteger(rd.readLine());
BigInteger low = BigInteger.ONE;
BigInteger high = p;
int iterations = 0;
boolean doingLowLow = true;
while(low.compareTo(high) < 0) {
++iterations;
BigInteger mid;
BigInteger lowlow;;
if(doingLowLow && (lowlow = low.multiply(low)).compareTo(high) < 0)
mid = lowlow;
else {
mid = low.add(high).divide(TWO);
doingLowLow = false;
}
BigInteger powered = mid.pow(n);
int cmp = powered.compareTo(p);
if(cmp == 0) {
low = high = mid;
break;
}
if(cmp < 0) {
if(low.equals(mid))
low = low.add(BigInteger.ONE);
else
low = mid;
}
else {
if(high.equals(mid))
high = high.subtract(BigInteger.ONE);
else
high = mid;
}
}
System.out.println(low);
System.err.println("Iterations: " + iterations);
totalIterations+=iterations;
}
System.err.println("Total: " + totalIterations);
}
}