//http://acm.pku.edu.cn/JudgeOnline/problem?id=2109
import java.math.*;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
BigInteger L=new BigInteger("1");
BigInteger R=new BigInteger("1000000000");
Scanner s = new Scanner(System.in);
while(s.hasNext())
{
int n=s.nextInt();
BigInteger p=s.nextBigInteger();
BigInteger l=L,r=R;
BigInteger ans=BigInteger.valueOf(-1);
while(l.compareTo(r)<=0)
{
BigInteger k=(l.add(r)).divide(BigInteger.valueOf(2));
BigInteger temp=k.pow(n);
if(p.compareTo(temp)==0)
{
ans=k;
break;
}
else if(p.compareTo(temp)>0)
{
l=k.add(BigInteger.ONE);
if(ans.compareTo(k)<0)ans=k;
}
else
{
r=k.subtract(BigInteger.ONE);
}
}
System.out.println(ans);
}
}
}
poj 2109 大数+二分搜索
最新推荐文章于 2019-02-17 16:54:54 发布
本文提供了一个使用Java实现的大数幂运算解决方案,通过二分查找法高效地计算给定基数的幂次方是否等于目标值。该程序适用于处理非常大的数字,例如在密码学中寻找特定的指数。
2346

被折叠的 条评论
为什么被折叠?



