POJ 1811 Prime Test

Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 32534   Accepted: 8557
Case Time Limit: 4000MS

Description

Given a big integer number, you are required to find out whether it's a prime number.

Input

The first line contains the number of test cases T (1 <= T <= 20 ), then the following T lines each contains an integer number N (2 <= N < 254).

Output

For each test case, if N is a prime number, output a line containing the word "Prime", otherwise, output a line containing the smallest prime factor of N.

Sample Input

2
5
10

Sample Output

Prime
2
 /*遇上一个题目不同,这个题目是输出最小的质因子*/
#include<iostream>
using namespace std;
#include<cstdio>
#define S 10
#include<cstdlib>
#include<ctime>
#define ll long long
ll cas, maxz;
ll read()
{
ll ans=;char c;
c=getchar();
while(c<''||c>'') c=getchar();
while(c>=''&&c<='')
{
ans=ans*+c-'';
c=getchar();
}
return ans;
}
ll quick_mul_mod(ll a,ll b,ll c)//a*b%c
{
ll ret=;
a%=c;b%=c;
while(b)
{
if(b&)
{
ret+=a;
ret%=c;
b--;
}
a<<=;
a%=c;
b>>=;
}
return ret;
}
ll gcd(ll a,ll b)
{
if(a==) return ;
if(a<) return gcd(-a,b);
if(b==)
return a;
return gcd(b,a%b);
}
ll Pollard_rho(ll x,ll c)
{
ll x1=rand()%(x-)+;
ll x2=x1;
int i=,k=;
while()
{
i++;
x1=(quick_mul_mod(x1,x1,x)+c)%x;
ll d=gcd(x2-x1,x);
if(d!=&&d!=x) return d;
if(x2==x1) return x;
if(i==k)
{
x2=x1;
k+=k;
}
} }
ll quick_mod(ll a,ll b,ll c)//ji suan a^b%c
{
ll ans=;
a%=c;
while(b)
{
if(b&)
{
b--;
ans=quick_mul_mod(ans,a,c);
}
b>>=;
a=quick_mul_mod(a,a,c);
}
return ans;
}
bool Miller_rabin(ll n)
{
if(n==) return true;
if(n<=||!(n&)) return false;
ll u=n-,t=;
while(!(u&))
{
u>>=;
t++;
}
for(int i=;i<S;++i)
{
ll x=rand()%(n-)+;
x=quick_mod(x,u,n);
for(int i=;i<=t;++i)
{
ll y=quick_mul_mod(x,x,n);
if(y==&&x!=&&x!=n-)
return false;
x=y;
}
if(x!=) return false;
}
return true;
}
void findpri(ll n)
{
if(n<=) return;
if(Miller_rabin(n))
{
if(n!=)
maxz=min(maxz,n);
return;
}
ll p=n;
while(p==n)
p=Pollard_rho(p,rand()%(n-)+);
findpri(p);
findpri(n/p);
}
int main()
{
srand(time());
cas=read();
while(cas--)
{
maxz=(<<)-;/*不知道为什么这个赋初值的最大值,只有赋值为小于等于(1<<31)-1才对,我的maxz明明是long long型的啊*/
ll n=read();
findpri(n);
if(Miller_rabin(n))
printf("Prime\n");
else printf("%lld\n",maxz);
}
return ;
}

Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test的更多相关文章

  1. 数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  2. POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)

    题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...

  3. poj 1811 Prime Test 大数素数测试+大数因子分解

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 27129   Accepted: 6713 Case ...

  4. Miller&&Pollard POJ 1811 Prime Test

    题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...

  5. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  6. POJ 1811 Prime Test( Pollard-rho整数分解经典题 )

    链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...

  7. POJ 1811 Prime Test 素性测试 分解素因子

    题意: 给你一个数n(n <= 2^54),判断n是不是素数,如果是输出Prime,否则输出n最小的素因子 解题思路: 自然数素性测试可以看看Matrix67的  素数与素性测试 素因子分解利用 ...

  8. POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)

    Description Given a big integer number, you are required to find out whether it's a prime number. In ...

  9. POJ 1811 Prime Test

    题意:对于一个大整数,判断是否质数,如果不是质数输出最小质因子. 解法:判断质数使用Miller-Rabin测试,分解质因子使用Pollard-Rho,Miller-Rabin测试用的红书模板,将测试 ...

随机推荐

  1. 工作中常用的Git操作--------(一)

    今天主要记录一下平常工作当中使用的git操作: 1.git的安装这里省略: 2.git的操作指令: 在项目开发中,经常是拉去经理已经搭建好的一个项目,也就是给我们一个git地址.比如:http://g ...

  2. es6新语法Object.assign()

    1.介绍 Object.assign用于对象的合并,将源对象的所有可枚举属性复制到目标对象,只拷贝源对象自身的属性继承属性补考呗 Object.assign(target,source1,...)第一 ...

  3. thinkphp中的验证器

  4. 架构师必须搞懂DNS【转】

    DNS,全称Domain Name System,即域名系统,搞清楚,它不是DNF地下城与勇士. DNS是怎么来的,我们知道要访问一个服务器的资源可以通过IP的形式访问,但IP地址比较难记,也不方便读 ...

  5. 生成器(generator)和迭代(iterable , iterator, iteration)

    在搞清楚Generator之前,我们先讨论一下 iterable , iterator, iteration 1.Iterable 我们知道,在Python中所有东西都是object, 比如说变量,容 ...

  6. eclipse maven jetty启动修改默认端口

    如何修改eclipse中的maven项目jetty服务器的默认端口那?网上有很多办法,但配置上都没有效果,最后找到了简单.简洁的解决办法,就是在eclipse的jetty启动命令后面加上以下内容 je ...

  7. LINUX gcc安装rpm包顺序

    rpm -ivh cpp-4.1.2-42.el5.i386.rpm rpm -ihv kernel-headers-2.6.18-92.el5.i386.rpm rpm -ivh glibc-hea ...

  8. Linux下的各类文件

    .a文件是静态链接库文件.所谓静态链接是指把要调用的函数或者过程链接到可执行文件中,成为可执行文件的一部分.当多个程序都调用相同函数时,内存中就会存在这个函数的多个拷贝,这样就浪费了宝贵的内存资源.. ...

  9. HDU 3533 Escape(BFS+预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3533 题目大意:给你一张n* m的地图,人在起点在(0,0)要到达终点(n,m)有k(k<=10 ...

  10. hdu 1848(SG函数)

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...