链接:http://acm.hdu.edu.cn/showproblem.php?

pid=3864

题意:给出一个数N(1<=N<10^18)。假设N仅仅有四个约数。就输出除1外的三个约数。

思路:大数的质因数分解仅仅能用随机算法Miller Rabin和Pollard_rho。在測试多的情况下正确率是由保证的。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <map>
  6. #include <cstdlib>
  7. #include <queue>
  8. #include <stack>
  9. #include <vector>
  10. #include <ctype.h>
  11. #include <algorithm>
  12. #include <string>
  13. #include <set>
  14. #include <ctime>
  15. #define PI acos(-1.0)
  16. #define INF 0x7fffffff
  17. #define eps 1e-8
  18. #define maxn 50005
  19. typedef __int64 LL;
  20. typedef unsigned long long ULL;
  21. using namespace std;
  22. LL Factor[100];
  23. int t=0;
  24. LL mul_mod(LL a,LL b,LL n)
  25. {
  26. a=a%n;
  27. b=b%n;
  28. LL s=0;
  29. while(b)
  30. {
  31. if(b&1)
  32. s=(s+a)%n;
  33. a=(a<<1)%n;
  34. b=b>>1;
  35. }
  36. return s;
  37. }
  38. LL pow_mod(LL a,LL b,LL n)//求a^b%n
  39. {
  40. a=a%n;
  41. LL s=1;
  42. while(b)
  43. {
  44. if(b&1)
  45. s=mul_mod(s,a,n);
  46. a=mul_mod(a,a,n);
  47. b=b>>1;
  48. }
  49. return s;
  50. }
  51. bool isPrime(LL n, LL times)
  52. {
  53. if(n==2)return 1;
  54. if(n<2||!(n&1))return 0;
  55. LL a, u=n-1, x, y;
  56. int t=0;
  57. while(u%2==0)
  58. {
  59. t++;
  60. u/=2;
  61. }
  62. srand(100);
  63. for(int i=0; i<times; i++)
  64. {
  65. a = rand() % (n-1) + 1;
  66. x = pow_mod(a, u, n);
  67. for(int j=0; j<t; j++)
  68. {
  69. y = mul_mod(x, x, n);
  70. if ( y == 1 && x != 1 && x != n-1 )
  71. return false; //must not
  72. x = y;
  73. }
  74. if( y!=1) return false;
  75. }
  76. return true;
  77. }
  78. LL gcd(LL a,LL b)
  79. {
  80. if(a==0) return 1;
  81. if(a<0) return gcd(-a,b);
  82. return b==0?a:gcd(b,a%b);
  83. }
  84. LL Pollard_rho(LL n,LL c)//Pollard_rho算法。找出n的因子
  85. {
  86. LL i=1,j,k=2,x,y,d,p;
  87. x=rand()%n;
  88. y=x;
  89. while(true)
  90. {
  91. i++;
  92. x=(mul_mod(x,x,n)+c)%n;
  93. if(y==x)return n;
  94. if(y>x)p=y-x;
  95. else p=x-y;
  96. d=gcd(p,n);
  97. if(d!=1&&d!=n)return d;
  98. if(i==k)
  99. {
  100. y=x;
  101. k+=k;
  102. }
  103. }
  104. }
  105. void factor(LL n)
  106. {
  107. if(isPrime(n,20))
  108. {
  109. Factor[t++]=n;
  110. return;
  111. }
  112. LL p=n;
  113. while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);
  114. factor(p);
  115. factor(n/p);
  116. }
  117. void solve(LL a)
  118. {
  119. if(a==1)
  120. {
  121. printf("is not a D_num\n");
  122. return;
  123. }
  124. t=0;
  125. factor(a);
  126. sort(Factor,Factor+t);
  127. if(t==2)
  128. {
  129. if(Factor[0]!=Factor[1])
  130. {
  131. printf("%I64d %I64d %I64d\n",Factor[0],Factor[1],a);
  132. }
  133. else
  134. printf("is not a D_num\n");
  135. }
  136. else if(t==3)
  137. {
  138. if(Factor[0]==Factor[1]&&Factor[1]==Factor[2])
  139. printf("%I64d %I64d %I64d\n",Factor[0],Factor[0]*Factor[1],a);
  140. else printf("is not a D_num\n");
  141. }
  142. else printf("is not a D_num\n");
  143. }
  144. int main()
  145. {
  146. LL a;
  147. while(~scanf("%I64d",&a))
  148. {
  149. solve(a);
  150. }
  151. return 0;
  152. }

HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解的更多相关文章

  1. Miller-Rabin 素性测试 与 Pollard Rho 大整数分解

    \(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...

  2. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  3. 整数(质因子)分解(Pollard rho大整数分解)

    整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...

  4. Pollard Rho大质数分解学习笔记

    目录 问题 流程 代码 生日悖论 end 问题 给定n,要求对n质因数分解 普通的试除法已经不能应用于大整数了,我们需要更快的算法 流程 大概就是找出\(n=c*d\) 如果\(c\)是素数,结束,不 ...

  5. 【HDU - 4344】Mark the Rope(大整数分解)

    BUPT2017 wintertraining(15) #8E 题意 长度为n(\(n<2^{63}\))的绳子,每隔长度L(1<L<n)做一次标记,标记值就是L,L是n的约数. 每 ...

  6. hdu 3864 D_num

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

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

  8. 大整数分解质因数(Pollard rho算法)

    #include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...

  9. Miller Rabin 详解 && 小清新数学题题解

    在做这道题之前,我们首先来尝试签到题. 签到题 我们定义一个函数:\(qiandao(x)\) 为小于等于 x 的数中与 x 不互质的数的个数.要求 \(\sum\limits _{i=l}^r qi ...

随机推荐

  1. Linux下清除系统日志方法

    摘要:相信大家都是用过Windows的人.对于Windows下饱受诟病的各种垃圾文件都需要自己想办法删除,不然你的系统将会变得越来越大,越来越迟钝!windows怎么清理垃圾相信大家都知道的,那么li ...

  2. Weka中数据挖掘与机器学习系列之基本概念(三)

    数据挖掘和机器学习 数据挖掘和机器学习这两项技术的关系非常密切.机器学习方法构成数据挖掘的核心,绝大多数数据挖掘技术都来自机器学习领域,数据挖掘又向机器学习提出新的要求和任务. 数据挖掘就是在数据中寻 ...

  3. 是时候抛弃web.xml了?

    你是否再为配置文件web.xml容易出错而烦恼?是否为web.xml文件存放位置而不知所措?是否为web.xml为什么要这样配?怎么才能更好的配置web.xml而烦恼?那么一种新的方式出现了: spr ...

  4. 轻松掌握Ubuntu Linux的3D桌面快捷键使用

    视频下载地址: http://115.com/file/be4n23v6#linux3d.rar 轻松掌握Ubuntu Linux的3D桌面快捷键使用 高级3D桌面展示 本文出自 "李晨光原 ...

  5. 【VC++学习笔记三】控件自绘

    MFC应用程序中,大部分的控件类型都已经被定制好了,即便是修改,也只是小范围内的修改,而很多情况下,我们又需要对界面进行特殊定制,这时,最好的办法就是用CWnd类进行派生,自己生成新的窗体,在WM_P ...

  6. TI Code Composer Studio MSP430系列驱动源代码

    一.参考TI官网驱动源代码 安装打开Code Composer Studio,如下图所示,最近在调试MSP430G2533的AD,需要参考官网的底层驱动配置. 从Code Composer Studi ...

  7. jQuery对表格进行类样式

    <%-- <%@ page language="java" contentType="text/html; charset=utf-8" pageE ...

  8. 4.Maven之(四)Maven命令

    转自:https://blog.csdn.net/u012152619/article/details/51473410

  9. js --- 事件冒泡 事件捕获

    先上结论: 他们是描述事件触发时序问题的术语.事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件.相反的,事件冒泡是自下而上的去触发事件.绑定事件方法的第三个参数,就是控制事 ...

  10. Android ijkplayer在windows下编译并导入Android Studio

     我是看着里面的步骤来做的,由于我自己对Linux环境和命令不熟悉,导致我对Cygwin的知识为零,在编译ijkplayer的时候走了一点弯路,需要的同学先去看一下上面的这篇文章,我这边是对上面文章做 ...