[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经典题 /************************************** ...
随机推荐
- 【BZOJ3864】Hero meet devil DP套DP
[BZOJ3864]Hero meet devil Description There is an old country and the king fell in love with a devil ...
- 九度OJ 1170:找最小数 (最值)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6451 解决:2843 题目描述: 第一行输入一个数n,1 <= n <= 1000,下面输入n行数据,每一行有两个数,分别是x ...
- JavaEE详解
本文主要讲JavaEE相关知识. 一 JavaEE 简介 JavaEE是很多技术的合集.提供了一套做B/S结构应用时,可能遇到问题的一套解决方案. 例如:处理客服端请求的servlet技术方案.处理数 ...
- Android笔记之ViewModel的使用示例
依赖 implementation 'android.arch.lifecycle:extensions:1.1.1' implementation 'com.squareup.retrofit2:r ...
- 前端几个笔试题及答案(bd)
1. 行内元素.块级元素和空元素(void)举例. 块级元素:<address>.<caption>.<dd>.<div>.<dl>.& ...
- python列表(list)常用方法
#!/usr/bin/env python # -*- coding:utf-8 -*- a = [1, 2, 3, 4, 5] # 索引 print(a[0], a[1], a[2], a[3], ...
- C语言实现 操作系统 银行家算法
/**************************************************** 银行家算法 算法思想: 1. 在多个进程中,挑选资源需求最小的进程Pmin. 可能存在多类资 ...
- 微信小程序配置详解
在之前已经通过微信公众平台的官方网站https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.html,注册好小程序并且登录成功后(这里主 ...
- CALL FUNCTION 'BAPI_PO_CREATE1' 相关报错
*&---------------------------------------------------------------------**& Report ZQJ06*&am ...
- 特殊例子--JavaScript代码实现图片循环滚动效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...