POJ 2109 Power of Cryptography大数,二分,テイラー定理難易度:2

2496 ワード

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
" , exp(log(n)/m) ) ; }

別添大神証明構想:タイラー公式証明の差は9を超えない
http://blog.csdn.net/synapse7/article/details/11672691