http://poj.org/problem?id=2109
Power of Cryptography
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 15969 | Accepted: 8065 |
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<10101 and there exists an integer k, 1<=k<=109 such that kn = 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 Output
4
3
1234
Source
México and Central America 2004
解析:
方法一:
就是给求指数幂,这里用到的一点巧办法,那就是直接用数学函数函数套用即可
float 32 6~7 10^(-37) ~ 10^38
double 64 15~16 10^(-307) ~ 10^308
long double 128 18~19 10^(-4931) ~ 10 ^ 4932
PS:原来浮点数的值可以这么大!!!!
#include<iostream>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
double n,p;
while(scanf("%lf%lf",&n,&p)!=EOF)
{
printf("%.0lf\n",pow(p,1.0/p));
}
//system("pause");
return 0;
}
方法二:
二分+高精度:
来自博客http://blog.csdn.net/code_pang/article/details/8263971
如果你只是想知道为什么double型开n次方法通过的原因,请跳过此部分。
题目大意:
求一个整数k,使得k满足kn=p。
思路:
由于p的值很大,超出了基本数据类型所表示的范围,所以采用大数乘法来计算kn,当kn=p时,得出结果(不知道能不能大数开n次方…)。那k值应该如何取呢?根据n和p的关系是可以确定出k的位数的,例如:n=7,p=4357186184021382204544,p的位数为22,用22/7的结果向上取整,得到4,即为k的位数,也就是说k的取值范围是1000~9999。在这个范围内进行二分查找,就可以找到满足条件的k值。(这狗屎题目中说“there exists an integer k, 1<=k<=109 , such that kn = p.”,其实…逗你玩!)
[cpp]view plaincopyprint?
1 #include <stdio.h>
2 #include <string.h>
3 #include <math.h>
4
5 #define LENGTH 110
6 #define LAST LENGTH-2
7 #define GREATER 1
8 #define EQUAL 0
9 #define LESS -1
10
11 //大整数相乘
12 char* IntegerMultiplication(const char *a, const char *b, char *product)
13 {
14 int i, j, k = LAST, first, value, temp[LENGTH];
15 memset(temp, 0, sizeof(temp));
16 for (i = strlen(a)-1; i >= 0; i--)
17 {
18 first = k--;
19 value = a[i] - '0';
20 for (j = strlen(b)-1; j >= 0; j--)
21 {
22 temp[first--] += value * (b[j] - '0');
23 }
24 }
25 for (i = LAST; i >= first; i--)
26 {
27 product[i] = temp[i] % 10 + '0';
28 temp[i-1] += temp[i] / 10;
29 }
30 while (product[first] == '0' && first < LAST)
31 {
32 first++;
33 }
34 return &product[first];
35 }
36
37 //比较两个字符串所表示数值的大小
38 int Compare(char *numA, char *numB)
39 {
40 //去除前导'0'
41 for (; *numA == '0'; numA++);
42 for (; *numB == '0'; numB++);
43 int lenNumA = strlen(numA);
44 int lenNumB = strlen(numB);
45 if (lenNumA == lenNumB)
46 {
47 return strcmp(numA, numB);
48 }
49 if (lenNumA > lenNumB)
50 {
51 return GREATER;
52 }
53 return LESS;
54 }
55
56 //求base^exp,结果存放在res中,pRes指向结果的首位数字的位置
57 char* Power(char *base, int exp, char *res)
58 {
59 res[LAST] = '1';
60 char *pRes = &res[LAST], *temp = base;
61 while (exp != 0)
62 {
63 if (exp % 2 == 1)
64 {
65 pRes = IntegerMultiplication(base, pRes, res);
66 }
67 exp /= 2;
68 if (exp != 0)
69 {
70 base = IntegerMultiplication(base, base, temp);
71 }
72 }
73 return pRes;
74 }
75
76 int main(void)
77 {
78 char p[LENGTH], res[LENGTH], cMid[LENGTH];
79 unsigned int n, lenP, lenRoot, i, min, max, mid;
80
81 while (scanf("%d %s", &n, &p) != EOF)
82 {
83 //根据n和p的倍数关系,得到k的范围的min值和max值
84 lenP = strlen(p);
85 lenRoot = (int)ceil((double)lenP / n);
86 for (i = 1, min = 1; i < lenRoot; i++)
87 {
88 min *= 10;
89 }
90 for (i = 1, max = 9; i < lenRoot; i++)
91 {
92 max *= 10;
93 max += 9;
94 }
95
96 //二分法寻找k值
97 bool finish = false;
98 while (!finish)
99 {
100 mid = (min + max) / 2;
101 if (min >= max)
102 {
103 break;
104 }
105 sprintf(cMid, "%d", mid);
106 memset(res, 0, sizeof(res));
107 switch (Compare(Power(cMid, n, res), p))
108 {
109 case LESS: min = mid + 1; break;
110 case EQUAL: finish = true; break;
111 case GREATER: max = mid - 1; break;
112 default: break;
113 }
114 }
115 //由于题目所给数据会有不满足k^n=p的情况
116 //下面是为了得到一个最大的k,满足k^n<=p
117 sprintf(cMid, "%d", mid);
118 if (Compare(Power(cMid, n, res), p) == GREATER)
119 {
120 mid--;
121 }
122 printf("%d\n", mid);
123 }
124 return 0;
125 }