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. py,pyc,pyw文件的区别和使用

    熟悉python编程的都知道,python三种最常见的py文件格式,.py,.pyc,.pyw,下面说一说它们各自的使用. py文件 python最常见的文件,是python项目的源码: 文件执行时l ...

  2. Linux系统中提示/usr/bin/ld: cannot find -lxxx错误的通用解决方法

    在linux环境编译应用程式或lib的source code时常常会出现如下的错误讯息: 代码如下: /usr/bin/ld: cannot find -lxxx 这些讯息会随着编译不同类型的sour ...

  3. GitHub vs GitLab:它们有什么区别?

    查看原文GitLab vs. GitHub: How Are They Different? 两者都是基于web的Git repositories(仓库),拥有流水线型的web开发流程,它们为开发团队 ...

  4. linux下rz,sz安装

    1.sz rz yum安装 yum install lrzsz

  5. Linux时间结构体和获得时间函数

    关于Linux下时间编程的问题: 1. Linux下与时间有关的结构体 struct timeval { int tv_sec; int tv_usec; }; 其中tv_sec是由凌晨开始算起的秒数 ...

  6. 结构体对齐及#pragma详细解释

    在linux下c语言结构体对齐: 1.自然对齐 struct 是一种复合数据类型,其构成元素既可以是基本数据类型(如int.long.float 等)的变量,也可以是一些复合数据类型(如array.s ...

  7. 基于layui+cropper.js实现上传图片的裁剪功能

    最近因项目需求,需要在上传图片的时候先对图片裁剪,然后在上传,所以就有了本文的出现. 开始正文之前,要提一下这个图片的裁剪:图片的裁剪,有前端裁剪,也可以后端裁剪 前端的裁剪我知道的可以分为这么两种: ...

  8. DNS之XX记录

    DNS服务器里有两个比较重要的记录.一个叫SOA记录(起始授权机构) 一个叫NS(Name Server)记录(域名服务器)关于这两个记录,很多文章都有解释,但是很多人还是很糊涂.我现在通俗的解释一下 ...

  9. Jmeter----请求的reponse结果中的某个参数作为JDBC Request的查询条件

    一.前言 数据库连接成功,若不会的查看:https://www.cnblogs.com/syw20170419/p/9832402.html 二.需求 将登录账号12608523691,接口的repo ...

  10. [翻译]HLS实践

    最近公司项目没事做,课余实践研究一下技术,算是积累,也可以用到项目里,从零开始记录 HLS:Http Live Streaming 官方文档 https://developer.apple.com/s ...