컴퓨터 - ACM ICPC
08/11/20 04:07(년/월/일 시:분)
http://acm.uva.es/p/v1/113.html
Power of Cryptography
1. k = n√p = p ^ (1/n)
2. 큰 정수를 입력받기 위해 BigInteger 사용.
3. BigInteger끼리 pow()가 안 되므로 double로 바꿔서.
4. double로 연산하면 오차가 생기니까 반올림.
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanKeyboard = new Scanner(System.in);
while(scanKeyboard.hasNextBigInteger())
{
BigInteger bigint_n = scanKeyboard.nextBigInteger();
BigInteger bigint_p = scanKeyboard.nextBigInteger();
double n = bigint_n.doubleValue();
double p = bigint_p.doubleValue();
double k = Math.pow(p,1/n);
long long_k = Math.round(k);
System.out.println(Long.toString(long_k));
}
}
}
Accepted JAVA 0.550 2008-11-20 03:03:17
아니면 처음부터 double로 받아도 된다. (근데 실행시간이 이게 더 오래걸린다. 왜???)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanKeyboard = new Scanner(System.in);
while(scanKeyboard.hasNextDouble())
{
double n = scanKeyboard.nextDouble();
double p = scanKeyboard.nextDouble();
long k = Math.round( Math.pow(p,1/n) );
System.out.println(Long.toString(k));
}
}
}
Accepted JAVA 0.590 2008-11-20 05:45:54
http://icpcres.ecs.baylor.edu/onlinejudge/
UVa Online Judge