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. Java简单爬虫(一)

    简单的说,爬虫的意思就是根据url访问请求,然后对返回的数据进行提取,获取对自己有用的信息.然后我们可以将这些有用的信息保存到数据库或者保存到文件中.如果我们手工一个一个访问提取非常慢,所以我们需要编 ...

  2. 阿里分布式开源框架DUBBO 入门+ 进阶+ 项目实战视频教程

    史诗级Java/JavaWeb学习资源免费分享 欢迎关注我的微信公众号:"Java面试通关手册"(坚持原创,分享各种Java学习资源,面试题,优质文章,以及企业级Java实战项目回 ...

  3. 深入解析Mysql 主从同步延迟原理及解决方案

    MySQL的主从同步是一个很成熟的架构,优点为:①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;②在从主服务器进行备份,避免备份期间影响主服务器服务;③当主服务器出现问题时,可以 ...

  4. libuv 一 环境搭建, hello TTY

    引言 - 一时心起, libuv linux 搭建 有一天突然想起来想写个动画. 找了一下 ui 库太大. 后面想起以前弄过的 libuv. 但发现 libuv 相关资料也很少. 所以就有了这些内容. ...

  5. UBuntu14.04 --vim安装YouCompleteMe插件

    说明 我电脑的系统参数(用 uname -a命令查看)如下: Linux avyn-Lenovo --generic #-Ubuntu SMP Tue Mar :: UTC i686 i686 i68 ...

  6. HDU-2222

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. 使用CEPH RGW admin ops API 进行用户user AK/SK管理的秘诀

    需求: 云平台面板上需要支持为不同的用户创建不同的RGW 的AK/SK用户秘钥,以完成对象存储的用户隔离,并可以管理bucket和查看bucket容量信息. 分析:查阅CEPH官网文档 S3 API  ...

  8. golang-goroutine和channel

    goroutine 在go语言中,每一个并发的执行单元叫做一个goroutine 这里说到并发,所以先解释一下并发和并行的概念: 并发:逻辑上具备同时处理多个任务的能力 并行:物理上在同一时刻执行多个 ...

  9. JAVA邻接矩阵实现拓扑排序

    由于一直不适用邻接表 ,现在先贴一段使用邻接矩阵实现图的拓扑排序以及判断有无回路的问题.自己做的图.将就看吧. package TopSort; import java.util.LinkedList ...

  10. postgresql 数据导入导出

    [转] 分类: postgresql2013-06-09 10:21 2486人阅读 评论(0) 收藏 举报 一.导出数据库及具体表 1.导出数据库:方式一:pg_dump  -U  postgres ...