题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3864

  题意:给定一个数n,求n的因子只有四个的情况。

  Miller_Rabin和Pollard_rho模板题,复杂度O(n^(1/4)),注意m^3=n的情况。

  1. //STATUS:C++_AC_62MS_232KB
  2. #include <functional>
  3. #include <algorithm>
  4. #include <iostream>
  5. //#include <ext/rope>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <iomanip>
  9. #include <numeric>
  10. #include <cstring>
  11. #include <cassert>
  12. #include <cstdio>
  13. #include <string>
  14. #include <vector>
  15. #include <bitset>
  16. #include <queue>
  17. #include <stack>
  18. #include <cmath>
  19. #include <ctime>
  20. #include <list>
  21. #include <set>
  22. #include <map>
  23. using namespace std;
  24. //#pragma comment(linker,"/STACK:102400000,102400000")
  25. //using namespace __gnu_cxx;
  26. //define
  27. #define pii pair<int,int>
  28. #define mem(a,b) memset(a,b,sizeof(a))
  29. #define lson l,mid,rt<<1
  30. #define rson mid+1,r,rt<<1|1
  31. #define PI acos(-1.0)
  32. //typedef
  33. typedef long long LL;
  34. typedef unsigned long long ULL;
  35. //const
  36. const int N=;
  37. const int INF=0x3f3f3f3f;
  38. const int MOD=,STA=;
  39. const LL LNF=1LL<<;
  40. const double EPS=1e-;
  41. const double OO=1e15;
  42. const int dx[]={-,,,};
  43. const int dy[]={,,,-};
  44. const int day[]={,,,,,,,,,,,,};
  45. //Daily Use ...
  46. inline int sign(double x){return (x>EPS)-(x<-EPS);}
  47. template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
  48. template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
  49. template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
  50. template<class T> inline T Min(T a,T b){return a<b?a:b;}
  51. template<class T> inline T Max(T a,T b){return a>b?a:b;}
  52. template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
  53. template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
  54. template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
  55. template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
  56. //End
  57.  
  58. LL factor[]; //质因数分解结果(刚返回时是无序的)
  59. int tol; //质因数的个数。数组小标从0开始
  60. const int S=;
  61.  
  62. LL gcd(LL a,LL b)
  63. {
  64. if(a==)return ;
  65. if(a<) return gcd(-a,b);
  66. while(b)
  67. {
  68. LL t=a%b;
  69. a=b;
  70. b=t;
  71. }
  72. return a;
  73. }
  74.  
  75. LL mult_mod(LL a,LL b,LL c)
  76. {
  77. a%=c;
  78. b%=c;
  79. LL ret=;
  80. while(b)
  81. {
  82. if(b&){ret+=a;ret%=c;}
  83. a<<=;
  84. if(a>=c)a%=c;
  85. b>>=;
  86. }
  87. return ret;
  88. }
  89.  
  90. //计算 x^n %c
  91. LL pow_mod(LL x,LL n,LL mod)//x^n%c
  92. {
  93. if(n==)return x%mod;
  94. x%=mod;
  95. LL tmp=x;
  96. LL ret=;
  97. while(n)
  98. {
  99. if(n&) ret=mult_mod(ret,tmp,mod);
  100. tmp=mult_mod(tmp,tmp,mod);
  101. n>>=;
  102. }
  103. return ret;
  104. }
  105. //以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
  106. //一定是合数返回true,不一定返回false
  107. bool check(LL a,LL n,LL x,LL t)
  108. {
  109. LL ret=pow_mod(a,x,n);
  110. LL last=ret;
  111. for(int i=;i<=t;i++)
  112. {
  113. ret=mult_mod(ret,ret,n);
  114. if(ret==&&last!=&&last!=n-) return true;//合数
  115. last=ret;
  116. }
  117. if(ret!=) return true;
  118. return false;
  119. }
  120.  
  121. // Miller_Rabin()算法素数判定
  122. //是素数返回true.(可能是伪素数,但概率极小)
  123. //合数返回false;
  124. bool Miller_Rabin(LL n)
  125. {
  126. if(n<)return false;
  127. if(n==)return true;
  128. if((n&)==) return false;//偶数
  129. LL x=n-;
  130. LL t=;
  131. while((x&)==){x>>=;t++;}
  132. for(int i=;i<S;i++)
  133. {
  134. LL a=rand()%(n-)+;//rand()需要stdlib.h头文件
  135. if(check(a,n,x,t))
  136. return false;//合数
  137. }
  138. return true;
  139. }
  140.  
  141. LL Pollard_rho(LL x,LL c)
  142. {
  143. LL i=,k=;
  144. LL x0=rand()%x;
  145. LL y=x0;
  146. while()
  147. {
  148. i++;
  149. x0=(mult_mod(x0,x0,x)+c)%x;
  150. LL d=gcd(y-x0,x);
  151. if(d!=&&d!=x) return d;
  152. if(y==x0) return x;
  153. if(i==k){y=x0;k+=k;}
  154. }
  155. }
  156. //对n进行素因子分解
  157. void findfac(LL n)
  158. {
  159. if(Miller_Rabin(n))//素数
  160. {
  161. factor[tol++]=n;
  162. return;
  163. }
  164. LL p=n;
  165. while(p>=n)p=Pollard_rho(p,rand()%(n-)+);
  166. findfac(p);
  167. findfac(n/p);
  168. }
  169.  
  170. LL n;
  171.  
  172. int main(){
  173. // freopen("in.txt","r",stdin);
  174. srand(time(NULL));
  175. int i,j;
  176. LL a,b;
  177. while(~scanf("%I64d",&n))
  178. {
  179. if(n==){
  180. printf("is not a D_num\n");
  181. continue;
  182. }
  183. tol=;
  184. findfac(n);
  185. if(tol!= && tol!=){
  186. printf("is not a D_num\n");
  187. continue;
  188. }
  189. sort(factor,factor+tol);
  190. if(tol== && factor[]!=factor[]){
  191. printf("%I64d %I64d %I64d\n",factor[],factor[],n);
  192. }
  193. else if(tol== && factor[]==factor[] && factor[]==factor[]){
  194. printf("%I64d %I64d %I64d\n",factor[],factor[]*factor[],n);
  195. }
  196. else printf("is not a D_num\n");
  197. }
  198. return ;
  199. }

HDU-3864 D_num Miller_Rabin和Pollard_rho的更多相关文章

  1. hdu 3864 D_num Pollard_rho算法和Miller_Rabin算法

    D_num Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...

  2. HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...

  3. hdu 3864 D_num

    思路:给一个数n,是否只有4个约数(包括1),也就是找3个大于1的约数. 而任何一个数都可由质数表示,所以对于给定的数,只需要进行质因数分解.这里有 2种情况:如果有3个一样的质因数,则满足条件:否则 ...

  4. hdu 3864 素数分解

    题意:求n是否只有4个因子,如果是就输出除1外的所有因子. 模板题,就不排版了 #include<cstdio> #include<iostream> #include< ...

  5. Miller_Rabin、 Pollard_rho Template

    Multiply and pow Function: //计算 (a*b)%c. a,b都是ll的数,直接相乘可能溢出的 // a,b,c <2^63 ll mult_modq(ll a,ll ...

  6. Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test

    POJ 1811 Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 32534   Accepted: 8 ...

  7. bzo4802 欧拉函数 miller_rabin pollard_rho

    欧拉函数 Time Limit: 5 Sec  Memory Limit: 256 MBSubmit: 1112  Solved: 418[Submit][Status][Discuss] Descr ...

  8. pollard_rho 学习总结 Miller_Rabbin 复习总结

    吐槽一下名字,泼辣的肉..OwO 我们知道分解出一个整数的所有质因子是O(sqrt(n)/ln(n))的 但是当n=10^18的时候就显得非常无力的 这个算法可以在大概O(n^(1/4))的时间复杂度 ...

  9. POJ 2429

    思路:a/n*b/n=lcm/gcd 所以这道题就是分解ans.dfs枚举每种素数情况.套Miller_Rabin和pollard_rho模板 //#pragma comment(linker, &q ...

随机推荐

  1. 通过AOP 实现异常统一管理

    package com.zhang.shine.cache; import java.lang.reflect.Method; import org.aspectj.lang.ProceedingJo ...

  2. 转TerreyLee AJAX入门系列2——ScriptManager的理解总结

    ScriptManager的功能之一就是处理页面上局部更新,对于这点,我想大家都知道.但是他工作的原理到底是什么呢,这个暂且不从正面来回答. 我们这样想一下,目前能够真正实现局部刷新的就是js+xml ...

  3. ios 使用GCD 多线程 教程

    什么是GCD Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法.该方法在Mac OS X 10.6雪豹中首次推出,并随后被引入到了iOS4.0中.GCD ...

  4. axis : java.lang.NoSuchMethodError

    Hi friends,Iam getting the following error when deploying my app in jboss error.Iam new to axis .can ...

  5. iOS开发:应用生命周期

    iOS应用通过委托对象AppDelegate类在应用周期的不同阶段会回调不同的方法,应用周期分为以下五种状态: Not Running(非运行状态).应用没有运行或被系统终止.   Inactive ...

  6. 为初学者写ORM,ORM的原理及测试案例

    提纲 一.什么是ORM.二.反射以及Attribute在ORM中的应用.三.创建一个数据库表和表对应的实体model.四.实体model如何映射出数据库表.五.组合ORM映射生成insert语句.六. ...

  7. Asp.net正则获取html内容

    1.获取div内容 string str = "tt<u>ss</u><div id=\"test\"><div>< ...

  8. sql server删除数据后空间无变化处理方法

    删除数据库表 第一步: 执行 delete from doc.115sou.com        #删除数据,执行效率低 drop table doc.115sou.com          #删除表 ...

  9. 统计nginx日志里流量

    用awk可以,比如,我想统计nginx日志里,今天下午3点0分,这一分钟内,访问的流量(文件的大小) grep "07/Nov/2013:15:00:"  *.log|awk '{ ...

  10. svn强制提交备注信息

    当我们用tortoisesvn,提交代码时,有很多人不喜欢写注释的,代码版本多了,根本搞不清,哪个版本改了什么东西?所以如果加一些注释的话,我们看起来,也方便很多.所以在提交的时候,我会强制要求,写注 ...