POJ 2109 Power of Cryptography 大数,二分,泰勒定理 难度:2

 

import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    static BigInteger p,l,r,div;
    static int n;
    public static int cmp(BigInteger mid){
        BigInteger sum=mid.pow(n);
        return sum.compareTo(p);
    }
    public static BigInteger calc(){
        l=BigInteger.ZERO;
        r=BigInteger.valueOf(1000000000);
        BigInteger div=BigInteger.valueOf(2);
        while(l.compareTo(r)<0){
            BigInteger mid=l.add(r).divide(div);
            int fl=cmp(mid);
            if(fl==0){
                return mid;
            }
            else if(fl==-1){
                l=mid.add(BigInteger.ONE);
            }
            else r=mid;
        }
        int fl=0;
        if((fl=cmp(r))==0)return r;
        if(fl==-1){
            while(p.subtract(r.pow(n)).compareTo(BigInteger.ONE)>0)r=r.add(BigInteger.ONE);
            return r;
        }
        else {
            while(r.pow(n).subtract(p).compareTo(BigInteger.ONE)>0)r=r.subtract(BigInteger.ONE);
            return r;
        }
    }
    public static void main(String args[]){
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
            n=scanner.nextInt();
            p=scanner.nextBigInteger();
            BigInteger ans=calc();
            System.out.println(ans);
        }
    }
}

 

有种更为优雅的姿势

#include <cstdio>
#include <cmath>

int main()
{
    double n , m ;
    int ans ;
    while ( scanf( "%lf%lf" , &m , &n ) != EOF )
          printf( "%.0f\n" , exp(log(n)/m) ) ;   
}

另附大神证明思路:泰勒公式证明相差不会超过9

http://blog.csdn.net/synapse7/article/details/11672691

posted @ 2014-08-28 10:24  雪溯  阅读(211)  评论(0编辑  收藏  举报