UVA 113 Power of cryptography

Source: Internet
Author: User
Tags pow

Original question:
Power of cryptography

Background

Cryptography involves (among other things) large prime numbers and computing powers of numbers modulo func tions of these primes. resulted in the practical use of results from number theory and other branches of mathematics once C Onsidered to is of only theoretical interest.

This problem involves the efficient computation of the integer roots of numbers.

The problem

Given an integer n>=1 and an integer p>=1 you is to write a program that determines p√n \sqrt[n]{p}, the positive Nth n^{th} root of P. In this problem, given such integers n and p, p'll always be the of the form kn k^n for a integer k (this integer was what y Our program must find).

The Input

The input consists of a sequence of integer pairs N and p with each integer in a line by itself. For all such pairs 1<=n<=200, 1<=p<= 10101 10^{101} and there exists an integer k, 1<=k<= 109 10^9 such That Kn=p k^n=p.

The Output

The For each integer pair n and p the value P√n \sqrt[n]{p} should is printed, i.e., the number k such that Kn=p k^n=p.

Sample Input

2
16
3
27
7
4357186184021382204544
Sample Output

4
3
1234

English:
give you n and p ask you how much k is. Among them kn=p k^n=p. The range of data is shown in English.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
Const LL MAXB=POW (10,9);
const double ep=10e-5;
Double n,p,k;
ll Bin_search ()
{
    ll l=1,r=maxb,mid;
    while (l<=r)
    {
        mid= (l+r)/2;
        K=pow (mid,n);
        if (Fabs (k-p) <ep)
            return mid;
        if (k<p)
            l=mid+1;
        else
            r=mid-1;
    }
    return 0;
}
int main ()
{
    Ios::sync_with_stdio (false);
    while (cin>>n>>p)
    {
        ll ans=bin_search ();
        cout<<ans<<endl;
    }
    return 0;
}

Answer:
The value of the K can be enumerated in two minutes. Just start looking at the data is very big, come up with high-precision template. The result is a variety of WA, because the high precision is not rounded. Later read Uvatoolkit said directly can use the POW function-_-!!! Then look at the next double representation range, and then use double instead of high precision.

It would be too boring to do this with POW (p,1/n).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.