[poj1811]Prime Test(Pollard-Rho大整数分解)
问题描述:素性测试兼质因子分解
解题关键:pollard-rho质因数分解,在RSA的破译中也起到了很大的作用
期望复杂度:$O({n^{\frac{1}{4}}})$
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<vector>
#define inf 1ll<<61
typedef long long ll;
using namespace std;
const int S=;
ll fac[],tol,mi;
ll mod_mul(ll a,ll b,ll p){
ll res=;
a%=p,b%=p;
while(b){
if(b&)res=(res+a)%p;
a=(a<<)%p;
b>>=;
}
return res;
}
ll mod_pow(ll x,ll n,ll p){
ll res=;
while(n){
if(n&)res=mod_mul(res,x,p);
x=mod_mul(x,x,p);
n>>=;
}
return res;
} bool check(ll a,ll n,ll x,ll t){//判断是否为合数
ll ret=mod_pow(a,x,n);
ll last=ret;
for(int i=;i<=t;i++){
ret=mod_mul(ret,ret,n);
if(ret==&&last!=&&last!=n-)return ;
last=ret;
}
if(ret!=)return ;//fermat测试
return ;
} bool Miller_Rabin(ll n){
if(n<)return ;
if(n==)return ;
if((n&)==)return ;
ll x=n-,t=;
while((x&)==)x>>=,t++;
for(int i=;i<S;i++){
ll a=rand()%(n-)+;
if(check(a,n,x,t))return ;//合数
}
return ;
} ll Pollard_Rho(ll n,ll c){//返回值n的因子
ll i=,j=,x=rand()%(n-)+,y=x;
while(){
i++,x=(mod_mul(x,x,n)+c)%n;
ll p=__gcd((y-x+n)%n,n);
if(p!=&&p!=n)return p;//p本身是合数,分解为本身就无意义了
if(y==x)return n;//循环节只有1,不符合条件,同时也判圈了
if(i==j)y=x,j<<=;//这里控制1步和2步
}
}
void find1(ll n,ll c){//找因子主体
if(n==) return;
if(Miller_Rabin(n)){
fac[tol++]=n;
mi=min(mi,n);
return;
}
ll p=n,k=c;
while(p>=n)p=Pollard_Rho(p,c--);//返回的是小于n但不一定为素数的因子
find1(p,k);
find1(n/p,k);
}
int main() {
int t;
scanf("%d",&t);
while(t--){
long long n;
scanf("%lld",&n);
mi=n;
if(Miller_Rabin(n)) cout<<"Prime"<<endl;
else{
find1(n,);
cout<<mi<<endl;
}
}
return ;
}
[poj1811]Prime Test(Pollard-Rho大整数分解)的更多相关文章
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
\(\\\) Miller-Rabin 素性测试 考虑如何检验一个数字是否为素数. 经典的试除法复杂度 \(O(\sqrt N)\) 适用于询问 \(N\le 10^{16}\) 的时候. 如果我们要 ...
- 整数(质因子)分解(Pollard rho大整数分解)
整数分解,又称质因子分解.在数学中,整数分解问题是指:给出一个正整数,将其写成几个素数的乘积的形式. (每个合数都可以写成几个质数相乘的形式,这几个质数就都叫做这个合数的质因数.) .试除法(适用于范 ...
- HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解
链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...
- Pollard Rho大质数分解学习笔记
目录 问题 流程 代码 生日悖论 end 问题 给定n,要求对n质因数分解 普通的试除法已经不能应用于大整数了,我们需要更快的算法 流程 大概就是找出\(n=c*d\) 如果\(c\)是素数,结束,不 ...
- 大整数分解质因数(Pollard rho算法)
#include <iostream> #include <cstring> #include <cstdlib> #include <stdio.h> ...
- 【HDU - 4344】Mark the Rope(大整数分解)
BUPT2017 wintertraining(15) #8E 题意 长度为n(\(n<2^{63}\))的绳子,每隔长度L(1<L<n)做一次标记,标记值就是L,L是n的约数. 每 ...
- POJ2429 GCD & LCM Inverse pollard_rho大整数分解
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and t ...
- POJ 1811 Prime Test( Pollard-rho整数分解经典题 )
链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...
随机推荐
- gdb ../sysdeps/i386/elf/start.S: No such file or directory
使用 gdb 调试的时候 输入 l 之后出现下列信息 (gdb) l 1 ../sysdeps/i386/elf/start.S: No such file or directory. in ../s ...
- 【BZOJ1146】[CTSC2008]网络管理Network 树状数组+DFS序+主席树
[BZOJ1146][CTSC2008]网络管理Network Description M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工 ...
- 【BZOJ3956】Count 主席树+单调栈
[BZOJ3956]Count Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N ...
- LRM-00109: could not open parameter file
SQL>startup ...
- 性能测试--yslow
YSlow YSlow可以对网站的页面进行分析,并告诉你为了提高网站性能,如何基于某些规则而进行优化. YSlow可以分析任何网站,并为每一个规则产生一个整体报告,如果页面可以进行优化,则YSlow会 ...
- java 线程 被相互排斥堵塞、检查中断演示样例解说----thinking java4
package org.rui.thread.block; /** * 被相互排斥堵塞 就像在interrupting.java中看到的,假设你偿试着在一个对象上调用其synchronized方法, ...
- SHA-1算法c语言实现
安全哈希算法(Secure Hash Algorithm)主要适用于数字签名标准 (Digital Signature Standard DSS)里面定义的数字签名算法(Digital Signatu ...
- hadoop磁盘空间不均衡的解决办法
hadoop集群在运行一段时间后,总是会出现某台机器的磁盘使用率特别高,有的使用率特别低,针对这种情况,hadoop提供了balancer工具调整磁盘负载 使用命令:start-balancer.sh ...
- BZOJ3295 [Cqoi2011]动态逆序对 —— CDQ分治
题目链接:https://vjudge.net/problem/HYSBZ-3295 3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec Memory Limit: 1 ...
- poj1753 Flip Game —— 二进制压缩 + dfs / bfs or 递推
题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submis ...