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 n th. 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

  1. 2 16
  2. 3 27
  3. 7 4357186184021382204544

Sample Output

  1. 4
  2. 3
  3. 1234
    题意:给出np(n<=200,p<=10^101),求方程k^n=pk的正整数解,保证k<=10^9
  4. 题解:这道神题传说有非常神奇的解fa

   然而并没有什么卵用,你只会收到一连串的WA

   该题的意图应该是贪心,至于怎么贪……わかない……

  1. 好吧,我太菜了,只能用最暴力的方法,设x^y=p
    对于y>n的解来说,x必然小于k
    对于y<n的解来说,x必然大于k
    对于x来说单调性
    所以可以二分
    至于y该怎么求……想必一个高精度的log就行了!而且只需要保留个位即可
    感觉我的代码还是有问题的,但莫名1A
    代码如下
  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<algorithm>
  6. using namespace std;
  7. struct big
  8. {
  9. int len;
  10. int num[];
  11. };
  12. int n;
  13. void trans(char* a,big &b)
  14. {
  15. memset(b.num,,sizeof(b.num));
  16. int len=strlen(a);
  17. for(int i=; i<len; i++)
  18. {
  19. b.num[len-i-]=a[i]-'';
  20. }
  21. b.len=len;
  22. }
  23. void trans_(int a,big &b)
  24. {
  25. memset(b.num,,sizeof(b.num));
  26. int len=;
  27. while(a)
  28. {
  29. b.num[len++]=a%;
  30. a/=;
  31. }
  32. b.len=len;
  33. }
  34. void print(big a)
  35. {
  36. for(int i=a.len-; i>=; i--)
  37. {
  38. printf("%d",a.num[i]);
  39. }
  40. puts("");
  41. }
  42. int comp(big x,big y)
  43. {
  44. if(x.len>y.len)
  45. {
  46. return ;
  47. }
  48. if(x.len<y.len)
  49. {
  50. return -;
  51. }
  52. for(int i=x.len-; i>=; i--)
  53. {
  54. if(x.num[i]>y.num[i])
  55. {
  56. return ;
  57. }
  58. if(x.num[i]<y.num[i])
  59. {
  60. return -;
  61. }
  62. }
  63. return ;
  64. }
  65. big sub(big a,big b)
  66. {
  67. big c;
  68. int len=a.len;
  69. int lenc=len;
  70. for(int i=; i<len; i++)
  71. {
  72. c.num[i]=a.num[i]-b.num[i];
  73. if(c.num[i]<)
  74. {
  75. c.num[i]+=;
  76. a.num[i+]--;
  77. }
  78. }
  79. while(c.num[lenc-]==&&lenc>)
  80. {
  81. lenc--;
  82. }
  83. c.len=lenc;
  84. return c;
  85. }
  86. void mul_ten(big &x)
  87. {
  88. int len=x.len;
  89. len++;
  90. for(int i=len-; i>=; i--)
  91. {
  92. x.num[i+]=x.num[i];
  93. }
  94. x.num[]=;
  95. while(x.num[len-]==&&len>)
  96. {
  97. len--;
  98. }
  99. x.len=len;
  100. }
  101. big div(big x,big y)
  102. {
  103. big f,m;
  104. memset(f.num,,sizeof(f.num));
  105. memset(m.num,,sizeof(m.num));
  106. m.len=;
  107. int len=x.len;
  108. for(int i=x.len-; i>=; i--)
  109. {
  110. mul_ten(m);
  111. m.num[]=x.num[i];
  112. while(comp(m,y)!=-)
  113. {
  114. m=sub(m,y);
  115. f.num[i]++;
  116. }
  117. }
  118. while(f.num[len-]==&&len>)
  119. {
  120. len--;
  121. }
  122. f.len=len;
  123. return f;
  124. }
  125. int check(big x,big y)
  126. {
  127. big z;
  128. int cnt=;
  129. z.len=;
  130. z.num[]=;
  131. while(!comp(x,z)==)
  132. {
  133. if(comp(x,y)==-)
  134. {
  135. break;
  136. }
  137. cnt++;
  138. x=div(x,y);
  139. }
  140. if(cnt<n)
  141. {
  142. return ;
  143. }
  144. else
  145. {
  146. return ;
  147. }
  148. }
  149. int main()
  150. {
  151. char b[];
  152. int a;
  153. big x,y;
  154. while(scanf("%d %s",&n,b)==)
  155. {
  156. memset(x.num,,sizeof(x.num));
  157. memset(y.num,,sizeof(y.num));
  158. int l=,r=;
  159. int mid;
  160. trans(b,y);
  161. while(l<r)
  162. {
  163. mid=(l+r)>>;
  164. trans_(mid,x);
  165. int flag=check(y,x);
  166. if(flag)
  167. {
  168. l=mid;
  169. }
  170. else
  171. {
  172. r=mid-;
  173. }
  174. if(r-l<=)
  175. {
  176. trans_(r,x);
  177. if(check(y,x))
  178. {
  179. printf("%d\n",r);
  180. break;
  181. }
  182. else
  183. {
  184. printf("%d\n",l);
  185. break;
  186. }
  187. }
  188. }
  189. }
  190. }
  1.  
  1.  

POJ - 2109 Power of Cryptography(高精度log+二分)的更多相关文章

  1. 贪心 POJ 2109 Power of Cryptography

    题目地址:http://poj.org/problem?id=2109 /* 题意:k ^ n = p,求k 1. double + pow:因为double装得下p,k = pow (p, 1 / ...

  2. POJ 2109 -- Power of Cryptography

    Power of Cryptography Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26622   Accepted: ...

  3. POJ 2109 Power of Cryptography 数学题 double和float精度和范围

    Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21354 Accepted: 107 ...

  4. poj 2109 Power of Cryptography

    点击打开链接 Power of Cryptography Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16388   Ac ...

  5. POJ 2109 Power of Cryptography【高精度+二分 Or double水过~~】

    题目链接: http://poj.org/problem?id=2109 参考: http://blog.csdn.net/code_pang/article/details/8263971 题意: ...

  6. POJ 2109 Power of Cryptography 大数,二分,泰勒定理 难度:2

    import java.math.BigInteger; import java.util.Scanner; public class Main { static BigInteger p,l,r,d ...

  7. poj 2109 Power of Cryptography (double 精度)

    题目:http://poj.org/problem?id=2109 题意:求一个整数k,使得k满足kn=p. 思路:exp()用来计算以e为底的x次方值,即ex值,然后将结果返回.log是自然对数,就 ...

  8. Poj 2109 / OpenJudge 2109 Power of Cryptography

    1.Link: http://poj.org/problem?id=2109 http://bailian.openjudge.cn/practice/2109/ 2.Content: Power o ...

  9. POJ-2109 Power of Cryptography(数学或二分+高精度)

    题目链接: https://vjudge.net/problem/POJ-2109 题目大意: 有指数函数 k^n = p , 其中k.n.p均为整数且 1<=k<=10^9 , 1< ...

随机推荐

  1. 你知道PORT吗?

    在TCP协议中,有端口(PORT)的概念,很多人都不知道端口到底是什么.之前介绍过物理地址,也就是网卡地址,做个不恰当的比喻,物理地址(MAC)地址,相当于身份证(唯一),家庭地址是几幢几单元相当于I ...

  2. Studio 3T 如何使用 Query Builder 查询数据

    Studio 3T 是一款对 MongoDB 进行数据操作的可视化工具. 在 Studio 3T 中,我们可以借助 Query Builder 的 Drag & Drop 来构建查询条件. 具 ...

  3. 【转】JMeter基础之——一个简单的性能测试

    上一节中,我们了解了jmeter的一此主要元件,那么这些元件如何使用到性能测试中呢.这一节创建一个简单的测试计划来使用这些元件.该计划对应的测试需求. 1)测试目标网站是fnng.cnblogs.co ...

  4. Effective java笔记3--类和接口1

    一.使类和成员的可访问能力最小化 要想区别一个设计良好的模块与一个设计不好的模块,最重要的因素是,这个模块对于外部的其他模块而言,是否隐藏了内部的数据和其他的实现细节.一个设计良好的模块会隐藏所有的实 ...

  5. Python web框架 flask

    Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请求并对请求进行预处理,然后 ...

  6. get与post两种方式的优缺点

    get: get是从服务器上获取数据,post是向服务器传送数据: get传送的数据量较小,不能大于2KB.post传送的数据量较大,一般被默认为不受限制.但理论上,IIS4中最大量为80KB,IIS ...

  7. DataTable改变column类型

    1.必须先克隆DataTable 2.列换类型 3.逐行往新DataTable赋值,并转换某列类型 如: DataTable dt = diorg.Clone(); //必须先克隆,此时并不包含数据 ...

  8. Maven整合SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  9. MySQL备份还原之一mydumper

    1)源码编译安装 1.下载 mydumper源码 2.解压 [mysql@localhost ~]$ tar -xvf mydumper-0.9.1.tar mydumper-0.9.1/CMakeL ...

  10. Python基础学习六 操作Redis

    import redis r = redis.Redis(host=',db=1) #set get delete setex 都是针对string类型的 k-v # r.set('louis_ses ...