POJ 1811 Prime Test( Pollard-rho整数分解经典题 )
**链接:****传送门 **
题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子
思路:Pollard-rho经典题
/*************************************************************************
> File Name: Pollard_rho_Test.cpp
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年05月24日 星期三 10时55分04秒
************************************************************************/
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
#define ll long long
#define TIME 30
const int MAX_N = 1000;
const int C = 201;
ll number = 0 , MIN ;
int List[MAX_N];
// --------------------Miller_Rabin素数测试-------------------------
ll RANDOM(ll n){ return (ll)((double)rand()/RAND_MAX * n + 0.5); }
ll quick_multi(ll a,ll b,ll Mod){ // 快速计算 a * b % Mod
ll ret = 0;
while(b){
if(b&1) ret = (ret + a) % Mod;
a = ( a << 1 ) % Mod;
b >>= 1;
}
return ret % Mod;
}
ll quick_pow(ll a,ll x,ll Mod){ // 快速计算 a^x % Mod
ll ret = 1;
while(x){
if(x&1) { ret = quick_multi( ret , a , Mod); x--; }
x >>= 1;
a = quick_multi( a , a , Mod );
}
return ret % Mod;
}
bool Witness(ll a,ll n){ // Miller_Rabin 二次探测
ll tmp = n - 1;
int j = 0;
while( !( tmp & 1 ) ){ tmp >>= 1; j++; }
ll x = quick_pow( a , tmp , n );
if( x == 1 || x == n-1 ) return false;
while( j-- ){
x = x*x % n ;
if( x == n-1 ) return false;
}
return true;
}
bool Miller_Rabin(ll n){ // 检测n是否为素数
if( n < 2 ) return false;
if( n == 2 ) return true;
if( !(n&1) ) return false;
for(int i = 1 ; i <= TIME ; i++){
ll a = RANDOM( n - 2 ) + 1; // 得到随机底数
if( Witness( a , n ) ) return false;
}
return true;
}
// --------------------Pollard_rho大整数分解-------------------------
ll gcd(ll a,ll b){ return b == 0 ? a : gcd( b , a%b ); }
ll Pollard_rho(ll n,ll c){ // 找到n的一个因子
ll x , y , d , i = 1 , k = 2;
x = RANDOM( n - 1 ) + 1;
y = x;
while(true){
i++;
x = ( quick_multi( x , x , n ) + c ) % n;
d = gcd( y - x , n );
if( 1 < d && d < n ) return d;
if( y == x ) return n;
if( i == k ){
y = x; k <<= 1;
}
}
}
void find(ll n,ll c){
if( n == 1 ) return;
if( Miller_Rabin(n) ){
List[ number++ ] = n ;
MIN = min( MIN , n );
return;
}
ll p = n ;
while( p >= n ) p = Pollard_rho( p , c-- );
find( p , c );
find( n/p , c );
}
int main(){
int T; ll tar , tmp = pow(2,54)-1;
scanf("%d",&T);
while(T--){
scanf("%lld",&tar);
if( Miller_Rabin(tar) ) printf("Prime\n");
else{
MIN = tmp;
find( tar , C );
printf("%lld\n",MIN);
}
}
return 0;
}
POJ 1811 Prime Test( Pollard-rho整数分解经典题 )的更多相关文章
- POJ 2429 GCD & LCM Inverse (Pollard rho整数分解+dfs枚举)
题意:给出a和b的gcd和lcm,让你求a和b.按升序输出a和b.若有多组满足条件的a和b,那么输出a+b最小的.思路:lcm=a*b/gcd lcm/gcd=a/gcd*b/gcd 可知a/gc ...
- POJ1811_Prime Test【Miller Rabin素数测试】【Pollar Rho整数分解】
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...
- POJ1811_Prime Test【Miller Rabin素数測试】【Pollar Rho整数分解】
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time ...
- POJ2429_GCD & LCM Inverse【Miller Rabin素数測试】【Pollar Rho整数分解】
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 ...
- HDU1164_Eddy's research I【Miller Rabin素数测试】【Pollar Rho整数分解】
Eddy's research I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test
POJ 1811 Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 32534 Accepted: 8 ...
- POJ 1811 Prime Test (Pollard rho 大整数分解)
题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...
- Miller&&Pollard POJ 1811 Prime Test
题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...
- POJ 1811 Prime Test 素性测试 分解素因子
题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的 素数与素性测试 素因子分解利用 ...
随机推荐
- SSH框架下单元测试的实现
SSH框架下单元测试的实现 实现的功能 实现了部门的增删改查 对Action进行了单元测试 对Service 进行了单元测试,通过mock的方式实现. 实现的步骤 一.对Action层的单元测试实现 ...
- UVa Problem 10051
这题有点类似LIS,由于颜色最多100种,所以只需建立一个100的数组,按对立面的关系以某种颜色为向上面的最大值就可以了. #include <iostream> #include & ...
- android蓝牙协议名词解释 OPP HFP HDP A2DP PAN
各种蓝牙协议的全称: OPP:对象存储规范(Object Push Profile),最为常见的,文件的传输都是使用此协议. HFP:(Hands-free Profile),让蓝牙设备能够控制电话, ...
- 使用excel进行数据挖掘(2)----分析关键影响因素
使用excel进行数据挖掘(2)----分析关键影响因素 在配置环境后,能够使用excel进行数据挖掘. 环境配置问题可參阅: http://blog.csdn.net/xinxing__8185/a ...
- Ext.TabPanel中的items具体解释
Ext.TabPanel中的items: (来自项目源代码中的items条目代码) items:{ id:"opt1", title:"默认页面", tabTi ...
- 这是一个文字游戏?“这个工作你们部门牵头xx”
近期集团一个部门在联系做一个网上应用平台系统.经过几次的会议沟通,这个原本就是解决取消个人银行卡收款的需求慢慢变成了一个在线销售加收款平台,因为其对销售的业务不熟悉,现有又有应用的软件,他们也感觉到主 ...
- Chromium多线程模型设计和实现分析
Chromium除了远近闻名的多进程架构之外,它的多线程模型也相当引人注目的.Chromium的多进程架构是为了解决网页的稳定性问题,而多线程模型则是为了解决网页的卡顿问题.为了达到这个目的,Chro ...
- 号外:java基础班教材永久免费 报名就送
以前万人疯抢的成都传智播客java基础班教材,今日免费赠送,你hold的住吗? 由成都传智播客传道授业解惑的诸位老师,精心制作的教材.如今免费赠送,你能接的住吗? 书是交融感情.获得知识.传承经验的重 ...
- SWERC13 Trending Topic
map暴力. .. Imagine you are in the hiring process for a company whose principal activity is the analys ...
- Qt5.8 提供 Apple tvOS,watchOS的技术预览版
New Platforms Apple tvOS (technology preview) Apple watchOS (technology preview) https://wiki.qt.io/ ...