k^n = p
n*log(k) = log(p)
log(k) = log(p)/n
k = exp(log(p)/n)
My code is as follows:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class Main {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str1 = "", str2 = "";
int n = 0;
BigInteger p = new BigInteger("0");
while (true) {
str1 = br.readLine();
if (str1 == null) {
break;
}
n = Integer.parseInt(str1);
str2 = br.readLine();
if (str2 == null) {
break;
}
p = new BigInteger(str2);
double lo = 0, hi = 10e9;
while (lo <= hi) {
boolean flag = false;
double mid = lo / 2 + hi / 2;
BigInteger temp = new BigInteger(Integer.toString((int) mid));
temp = temp.pow(n);
int comp = temp.compareTo(p);
switch (comp) {
case 0:
flag = true;
System.out.println((int) mid);
break;
case 1:
hi = (int) mid - 1;
break;
case -1:
lo = (int) mid + 1;
break;
}
if (flag) {
break;
}
}
}
}
}
No comments:
Post a Comment