I was doing a problem in which you are given a number P and a number N. You have to find a number K such that K^N is equal to P.
CONSTRAINTS ARE : 1 ≤ n ≤ 200, 1 ≤ p < 10^101
The python code that got AC :
while True:
try:
n = int(input())
p = int(input())
print(round(p**(1/n)))
except:
break
Why do I have to use the round function why can't I use ceil or floor?
When should I use round?
These kinds of questions cost me a lot of WAs. Can someone tell me how to deal with these kinds of questions?
My guess would be that
round
is more accurate thanceil
orfloor
sinceround
rounds to the nearest integer. However, to be safe, I wouldn't useround
or any floating-points here at all. Instead, you should do binary search onk
in order to find the exact integer answer. This requires using big integers, but big integers are built into Python anyway and it does not give you the precision problems that floating-points will give you.