Power of cryptographyTime
limit:MS
Memory Limit:0KB
64bit IO Format:%lld & %llu SubmitStatus
Description
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 integers and an integers you is to write a program that determines, the positive root of P. In this problem, given such integers n and p, p 'll always be of the form for an integer K (This integer is the what your 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, and there exists a integer K, such that.
The Output
The For each integer pair n and p The value should is printed, i.e., the number K such that.
Sample Input
21632774357186184021382204544
Sample Output
431234
Test instructions: Give your n,p,n k to be equal to P, let you beg K
Idea: First you need to know the limit values commonly used in ACM
Both int and long use 32 bits to store the maximum and minimum values of 2147483647 (109), respectively-2147483648;
A long long is 64 bits to store the maximum and minimum values of 9223372036854775807 (1018), respectively-9223372036854775808;
The maximum and minimum values of float are 3.40282e+038 (1038), 1.17549e-038 (10-38) respectively;
The maximum and minimum values for double are 1.79769e+308 (10308), 2.22507e-308 (10-308), respectively
The topic gives n in 10^108, so with double
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h>int main () { Double k,p,n; while (~scanf ("%lf%lf", &n,&p)) { int low,high,mid; low=1; High=pow (10,9); while (Low<=high) { mid= (Low+high)/2; K=pow (mid,n); if (k==p) { printf ("%d\n", mid); break; } else if (k<p) low=mid+1; else high=mid-1; } } return 0;}
See the online Daniel Practice, it feels amazing, hereby worship
Test instructions: Given N and P, find out, but p can be very large ()
How to store P? Don't you want a big number?
Look at the double row first: The index range is between -307~308 (base 10) and the valid number is 15 bits.
Error Analysis:
Order f (P) =p^ (1/n), Δ=f (P+δp)-F (P)
By the Taylor formula.
(the upper bound of Δp is because the precision of double is 15 bits, and n is the lower bound because)
It is known from the above that when the δp is maximum, the error is greatest when n is the smallest.
According to the scope of the topic, the error formula is δ<9.0e-7, indicating that the double is sufficient (this is from the aspect of the effective number 15 bit or relatively sufficient (relative to the float), which is a very small reason for float)
This satisfies the topic request, therefore may use double to cross this question.
#include <stdio.h> #include <math.h>int main () { double A, b; while (scanf ("%lf%lf", &a,&b)! = EOF) printf ("%.0lf\n", pow (b,1/a)) return 0;}
UVA 113-power of cryptography (binary +double processing big data)