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的 素数与素性测试 素因子分解利用 ...
随机推荐
- 0919MYSQL中取当前周/月/季/年的第一天与最后一天
http://blog.csdn.net/cleanfield/article/details/41447585 整理后的sql代码,全部可执行 #当年第一天: SELECT DATE_SUB(CUR ...
- 【ACM】hdu_zs3_1005_String Matching_201308100920
String Matching Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)Tota ...
- POJ 2167 Irrelevant Elements 质因数分解
Irrelevant Elements Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2231 Accepted: 55 ...
- Swift学习——类的定义,使用,继承,构造等(五)
Swift学习--类的定义,使用.继承,构造等(五) 类的使用说明 1 使用class和类名来创建一个类名,比如: class student 2 类中属性的声明和常量和变量一样,唯一的差别就是他们的 ...
- 怎样安装g++/gdb
该文的word文档附在文后! 链接:http://pan.baidu.com/s/1bnFcMHDpassword:z7zk
- Codeforces Round #377 (Div. 2) D. Exams
Codeforces Round #377 (Div. 2) D. Exams 题意:给你n个考试科目编号1~n以及他们所需要的复习时间ai;(复习时间不一定要连续的,可以分开,只要复习够ai天 ...
- Unity3d数据加密
在unity中能够使用c#自带的对称算法对数据进行加密,以下两种加密算法: using System; using System.Text; using System.Security.Cryptog ...
- HDU oj A + B Problem II
郁闷了就相同的代码在HDUOJ上提交就是AC在NYOJ上提交就是WA字符串处理 #include<stdio.h> #include<string.h> #define N 1 ...
- 再探Linux动态链接 -- 关于动态库的基础知识(Dynamic Linking on Linux Revisited)
在近一段时间里,由于多次参与相关专业软件Linux运行环境建设,深感有必要将这些知识理一理,供往后参考. 编译时和运行时 纵观程序编译整个过程,细分可分为编译(Compiling,指的是语言到平台 ...
- 【iOS开发-80】Quartz2D画图简单介绍:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...