HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
链接:http://acm.hdu.edu.cn/showproblem.php?
题意:给出一个数N(1<=N<10^18)。假设N仅仅有四个约数。就输出除1外的三个约数。
思路:大数的质因数分解仅仅能用随机算法Miller Rabin和Pollard_rho。在測试多的情况下正确率是由保证的。
代码:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <map>
- #include <cstdlib>
- #include <queue>
- #include <stack>
- #include <vector>
- #include <ctype.h>
- #include <algorithm>
- #include <string>
- #include <set>
- #include <ctime>
- #define PI acos(-1.0)
- #define INF 0x7fffffff
- #define eps 1e-8
- #define maxn 50005
- typedef __int64 LL;
- typedef unsigned long long ULL;
- using namespace std;
- LL Factor[100];
- int t=0;
- LL mul_mod(LL a,LL b,LL n)
- {
- a=a%n;
- b=b%n;
- LL s=0;
- while(b)
- {
- if(b&1)
- s=(s+a)%n;
- a=(a<<1)%n;
- b=b>>1;
- }
- return s;
- }
- LL pow_mod(LL a,LL b,LL n)//求a^b%n
- {
- a=a%n;
- LL s=1;
- while(b)
- {
- if(b&1)
- s=mul_mod(s,a,n);
- a=mul_mod(a,a,n);
- b=b>>1;
- }
- return s;
- }
- bool isPrime(LL n, LL times)
- {
- if(n==2)return 1;
- if(n<2||!(n&1))return 0;
- LL a, u=n-1, x, y;
- int t=0;
- while(u%2==0)
- {
- t++;
- u/=2;
- }
- srand(100);
- for(int i=0; i<times; i++)
- {
- a = rand() % (n-1) + 1;
- x = pow_mod(a, u, n);
- for(int j=0; j<t; j++)
- {
- y = mul_mod(x, x, n);
- if ( y == 1 && x != 1 && x != n-1 )
- return false; //must not
- x = y;
- }
- if( y!=1) return false;
- }
- return true;
- }
- LL gcd(LL a,LL b)
- {
- if(a==0) return 1;
- if(a<0) return gcd(-a,b);
- return b==0?a:gcd(b,a%b);
- }
- LL Pollard_rho(LL n,LL c)//Pollard_rho算法。找出n的因子
- {
- LL i=1,j,k=2,x,y,d,p;
- x=rand()%n;
- y=x;
- while(true)
- {
- i++;
- x=(mul_mod(x,x,n)+c)%n;
- if(y==x)return n;
- if(y>x)p=y-x;
- else p=x-y;
- d=gcd(p,n);
- if(d!=1&&d!=n)return d;
- if(i==k)
- {
- y=x;
- k+=k;
- }
- }
- }
- void factor(LL n)
- {
- if(isPrime(n,20))
- {
- Factor[t++]=n;
- return;
- }
- LL p=n;
- while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);
- factor(p);
- factor(n/p);
- }
- void solve(LL a)
- {
- if(a==1)
- {
- printf("is not a D_num\n");
- return;
- }
- t=0;
- factor(a);
- sort(Factor,Factor+t);
- if(t==2)
- {
- if(Factor[0]!=Factor[1])
- {
- printf("%I64d %I64d %I64d\n",Factor[0],Factor[1],a);
- }
- else
- printf("is not a D_num\n");
- }
- else if(t==3)
- {
- if(Factor[0]==Factor[1]&&Factor[1]==Factor[2])
- printf("%I64d %I64d %I64d\n",Factor[0],Factor[0]*Factor[1],a);
- else printf("is not a D_num\n");
- }
- else printf("is not a D_num\n");
- }
- int main()
- {
- LL a;
- while(~scanf("%I64d",&a))
- {
- solve(a);
- }
- return 0;
- }
HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解的更多相关文章
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- 整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...
- Pollard Rho大质数分解学习笔记
目录 问题 流程 代码 生日悖论 end 问题 给定n,要求对n质因数分解 普通的试除法已经不能应用于大整数了,我们需要更快的算法 流程 大概就是找出\(n=c*d\) 如果\(c\)是素数,结束,不 ...
- 【HDU - 4344】Mark the Rope(大整数分解)
BUPT2017 wintertraining(15) #8E 题意 长度为n(\(n<2^{63}\))的绳子,每隔长度L(1<L<n)做一次标记,标记值就是L,L是n的约数. 每 ...
- hdu 3864 D_num
思路:给一个数n,是否只有4个约数(包括1),也就是找3个大于1的约数. 而任何一个数都可由质数表示,所以对于给定的数,只需要进行质因数分解.这里有 2种情况:如果有3个一样的质因数,则满足条件:否则 ...
- 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 ...
- 大整数分解质因数(Pollard rho算法)
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...
- Miller Rabin 详解 && 小清新数学题题解
在做这道题之前,我们首先来尝试签到题. 签到题 我们定义一个函数:\(qiandao(x)\) 为小于等于 x 的数中与 x 不互质的数的个数.要求 \(\sum\limits _{i=l}^r qi ...
随机推荐
- Linux下清除系统日志方法
摘要:相信大家都是用过Windows的人.对于Windows下饱受诟病的各种垃圾文件都需要自己想办法删除,不然你的系统将会变得越来越大,越来越迟钝!windows怎么清理垃圾相信大家都知道的,那么li ...
- Weka中数据挖掘与机器学习系列之基本概念(三)
数据挖掘和机器学习 数据挖掘和机器学习这两项技术的关系非常密切.机器学习方法构成数据挖掘的核心,绝大多数数据挖掘技术都来自机器学习领域,数据挖掘又向机器学习提出新的要求和任务. 数据挖掘就是在数据中寻 ...
- 是时候抛弃web.xml了?
你是否再为配置文件web.xml容易出错而烦恼?是否为web.xml文件存放位置而不知所措?是否为web.xml为什么要这样配?怎么才能更好的配置web.xml而烦恼?那么一种新的方式出现了: spr ...
- 轻松掌握Ubuntu Linux的3D桌面快捷键使用
视频下载地址: http://115.com/file/be4n23v6#linux3d.rar 轻松掌握Ubuntu Linux的3D桌面快捷键使用 高级3D桌面展示 本文出自 "李晨光原 ...
- 【VC++学习笔记三】控件自绘
MFC应用程序中,大部分的控件类型都已经被定制好了,即便是修改,也只是小范围内的修改,而很多情况下,我们又需要对界面进行特殊定制,这时,最好的办法就是用CWnd类进行派生,自己生成新的窗体,在WM_P ...
- TI Code Composer Studio MSP430系列驱动源代码
一.参考TI官网驱动源代码 安装打开Code Composer Studio,如下图所示,最近在调试MSP430G2533的AD,需要参考官网的底层驱动配置. 从Code Composer Studi ...
- jQuery对表格进行类样式
<%-- <%@ page language="java" contentType="text/html; charset=utf-8" pageE ...
- 4.Maven之(四)Maven命令
转自:https://blog.csdn.net/u012152619/article/details/51473410
- js --- 事件冒泡 事件捕获
先上结论: 他们是描述事件触发时序问题的术语.事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件.相反的,事件冒泡是自下而上的去触发事件.绑定事件方法的第三个参数,就是控制事 ...
- Android ijkplayer在windows下编译并导入Android Studio
我是看着里面的步骤来做的,由于我自己对Linux环境和命令不熟悉,导致我对Cygwin的知识为零,在编译ijkplayer的时候走了一点弯路,需要的同学先去看一下上面的这篇文章,我这边是对上面文章做 ...