HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
链接:http://acm.hdu.edu.cn/showproblem.php?
pid=3864
题意:给出一个数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 内核源码(kernel source)
查看内核的发行版:uname -r(--kernel-release) $ uname -r 4.4.0-78-generic 内核源码所在的位置:/usr/src $ cd /usr/src $ l ...
- django 笔记3
FBV function base view url.py index -> 函数名 view.py def 函数(requset): ... CBV class base view /inde ...
- 2015上海网络赛 HDU 5478 Can you find it 数学
HDU 5478 Can you find it 题意略. 思路:先求出n = 1 时候满足条件的(a,b), 最多只有20W对,然后对每一对进行循环节判断即可 #include <iostre ...
- SQL Server 多种分页查询效率
关于SQL语句分页,网上也有很多,我贴一部分过来,并且总结自己已知的分页到下面,方便日后查阅. 方法1 适用于 SQL Server 任何版本 SELECT TOP 页大小 * FROM table1 ...
- Windows Server 2016 辅助域控制器搭建
Windows Server 2016 主域控制器搭建完成后,继续进行辅助域控制器搭建.1.更改服务器的IP地址2.修改服务器的名称3.打开服务器管理器,选择添加角色和功能4.选择,下一步5.选择,下 ...
- Linux 下查看某进程的线程数
1.查看文件 /proc/${pid}/status2.pstree -p ${pid}3.输入 top -bH -d 3 -p ${pid}top -H手册中说:-H : Threads toggl ...
- iterator的使用和封个问题
这篇文章的内容还是不错的: http://www.cnblogs.com/zhuyf87/archive/2012/12/08/2808290.html for (vector<int>: ...
- HDU4596 Yet another end of the world 扩展欧几里德性质
这题坑了,我真该吃翔啊,竟然一開始方程设错了并且没有去想连列的问题,我真是坑货,做不出就该又一次理一下嘛.操蛋. 题意:给了N组x,y,z然后 问你是否存在两个或者两个以上的id,是的 id%x的值在 ...
- ZOJ 2562 HDU 4228 反素数
反素数: 对于不论什么正整数x,起约数的个数记做g(x).比如g(1)=1,g(6)=4. 假设某个正整数x满足:对于随意i(0<i<x),都有g(i)<g(x),则称x为反素数. ...
- [DLX反复覆盖] hdu 2828 Lamp
题意: 有N个灯M个开关 每一个灯的ON和OFF状态都能控制一个灯是否亮 给出N行,代表对于每一个灯 哪些开关的哪个状态能够使得第i个灯亮 思路: 这里须要注意一个问题 假设开关1的ON 状态和开关2 ...