(转) 二分法+高精度——Poj 2109 Power of Cryptography(double型开n次方的方法通过的原因)

时间:2022-08-07 23:25:16
Power of Cryptography
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 14584   Accepted: 7412

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


如果你只是想知道为什么double型开n次方法通过的原因,请跳过此部分。

题目大意:

求一个整数k,使得k满足kn=p。

思路:

由于p的值很大,超出了基本数据类型所表示的范围,所以采用大数乘法来计算kn,当k­n=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 plain copy print?
  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 = truebreak;  
  111.             case GREATER: max = mid - 1; break;  
  112.             defaultbreak;  
  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. }  

double型开n次方的方法通过的原因

下面这段程序也是可以通过此题的。

[cpp] view plain copy print?
  1. #include<stdio.h>  
  2. #include<math.h>  
  3.   
  4. int main(void)  
  5. {  
  6.     double n, p;  
  7.     while(scanf("%lf%lf", &n, &p) != EOF)  
  8.     {  
  9.         printf("%.0lf\n", pow(p, 1/n));  
  10.     }  
  11.     return 0;  
  12. }  

首先,题目中的数据强度并不弱,这一点确实如题目中所说:“For all such pairs 1<=n<= 200, 1<=p<10101,所以,double型是不能精确地表示出所给数据,但是却能表示出一个近似值。

当向double型变量中存入

4357186184021382204544

然后再输出,得到的是

4357186184021382000000

后六位的值变为了0,这一点和int型变量是有很大区别的。也就是说当存入double型变量的值超出了它的精度表示范围时,将低位的数据截断。(关于浮点数在计算机中的表示方法,百度吧…讲的蛮清楚的。)

在本题中,如果测试数据为:

7     4357186184021382204544

实际上所处理数据是:

7     4357186184021382000000

拿4357186184021382000000开7次方的结果自然就是1234。

为什么不是1233或者1235呢?

12337=4332529576639313702577

12347=4357186184021382204544

12357=4381962969567270546875

可以看出在double型所能表示的精度范围内,它们三个值已经不同了。

所以,此题中的测试数据也都是类似于上述情况,所以才能使用double型开n次方的方法。


原博主地址:点击打开链接