poj2109

Description

Current work in cryptography involves (among other things) large prime numbers and computing powers of numbers among these primes. Work in this area has resulted in the practical use of results from number theory and other branches of mathematics once considered to be only of theoretical interest. This problem involves the efficient computation of integer roots of numbers. Given an integer n>=1 and an integer p>= 1 you have to write a program that determines the n th positive root of p. In this problem, given such integers n and p, p will always be of the form k to the nth. power, for an integer k (this integer is what your program must find).

Input

The input consists of a sequence of integer pairs n and p with each integer on a line by itself. For all such pairs 1<=n<= 200, 1<=p<10^101 and there exists an integer k, 1<=k<=10^9 such that k^n= p.

Output

For each integer pair n and p the value k should be printed, i.e., the number k such that k^n =p.

Sample Input

2 16
3 27
7 4357186184021382204544

**Sample Outpu

4
3
1234

大概意思是

  • 想求满足k^n=p中的k值,

其实是想用二分法高精度算法的思想来考查,但是p=10^101并没有超出double的精度。
使用指数的倒数开n次方,这时就可以用pow函数了,k=pow(p,1.0/n),double的运算一步到位,k自然也是一个int。为了锻炼水品就用第一种方法了:

以下代码主要思想是:

  1. 用mid^n和p比较,如果mid^n小于p,则ks+1;如果mid^n大于p,则kl-1;
    其中mid=(ks+kl)/2。这基本就是二分法的思想。

  2. 以下代码用到了大整数的类方法,包括compareTo,add,divide,subtract,具体信息可以参考java.math.BigInteger的API网址
    http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    static final BigInteger TWO = new BigInteger("2");
    //因为BigInteger只有0,1,10,构造方法类型有限,只能有字符串2,然后转化为BigInteger类型了。
    static final BigInteger ONE = BigInteger.ONE;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // k^n=p findK;
          BigInteger p;
          int n;
        BigInteger ks, kl;
        while (in.hasNextInt()) {//只要有整数就输入给了in
            boolean find=false;//找到标志位
            n = in.nextInt();//将第一个数转换为int类型给了n
            p = in.nextBigInteger();//将第二个数转换为BigInteger类型给了p
            ks = ONE;
            kl = p;
            while (ks.compareTo(kl) <= 0) {
                BigInteger mid = ks.add(kl).divide(TWO);
                int ret = mid.pow(n).compareTo(p);
                if (ret == 0) {
                    find=true;
                    System.out.println(mid);
                    break;
                } else if (ret == -1) {
                    ks = mid.add(ONE);
                } else {
                    kl = mid.subtract(ONE);
                }
            }
            if(!find){
                System.out.println(ks.subtract(ONE));
            }
        }
    }
}

BigInteger构造函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值