Prime Test
Time Limit: 6000MS   Memory Limit: 65536K
Total Submissions: 27129   Accepted: 6713
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

Source

 
 
 
 
 /*
给你一个大数,如果是素数输出prime,如果不是,则输出其最小的质因子。 利用Miller-Rabbin素数测试和Pollar-rho因数分解可以AC这道题 */ #include<iostream>
#include<cstdio>
#include<string.h>
#include<time.h>
#include<algorithm>
using namespace std; typedef __int64 LL;
//****************************************************************
// Miller_Rabin 算法进行素数测试
//速度快,而且可以判断 <2^63的数
//****************************************************************
const int S=;//随机算法判定次数,S越大,判错概率越小 LL mult_mod(LL a,LL b,LL mod) //(a*b)%c a,b,c<2^63
{
a%=mod;
b%=mod;
LL ans=;
while(b)
{
if(b&)
{
ans=ans+a;
if(ans>=mod)
ans=ans-mod;
}
a=a<<;
if(a>=mod) a=a-mod;
b=b>>;
}
return ans;
} LL pow_mod(LL a,LL b,LL mod) // a^b%mod
{
LL ans=;
a=a%mod;
while(b)
{
if(b&)
{
ans=mult_mod(ans,a,mod);
}
a=mult_mod(a,a,mod);
b=b>>;
}
return ans;
} //以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false bool check(LL a,LL n,LL x,LL t)
{
LL ret=pow_mod(a,x,n);
LL last=ret;
for(int i=;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret== && last!= && last!=n-) return true;//合数
last=ret;
}
if(ret!=) return true;
else return false;
} // Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false; bool Miller_Rabin(long long n)
{
if(n<)return false;
if(n==) return true;
if( (n&)==) return false;//偶数
LL x=n-;
LL t=;
while( (x&)== ) { x>>=;t++;}
for(int i=;i<S;i++)
{
LL a=rand()%(n-)+;//rand()需要stdlib.h头文件
if(check(a,n,x,t))
return false;//合数
}
return true;
} //************************************************
//pollard_rho 算法进行质因数分解
//************************************************ LL factor[];//质因数分解结果(刚返回时是无序的)
int tol;////质因数的个数。数组小标从0开始 LL gcd(LL a,LL b)
{
if(a==) return ;// !!!!
if(a<) return gcd(-a,b);
while(b)
{
LL t=a%b;
a=b;
b=t;
}
return a;
} LL Pollard_rho(LL x,LL c)
{
LL i=,k=;
LL x0=rand()%x;
LL y=x0;
while()
{
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
LL d=gcd(y-x0,x);
if(d!= && d!=x) return d;
if(y==x0) return x;
if(i==k) {y=x0;k+=k;}
}
} //对n进行素因子分解 void findfac(LL n)
{
if(Miller_Rabin(n))
{
factor[tol++]=n;
return;
}
LL p=n;
while(p>=n)
p=Pollard_rho(p,rand()%(n-)+);
findfac(p);
findfac(n/p);
} int main()
{
// srand(time(NULL));//需要time.h头文件 //POJ上G++要去掉这句话
int T;
LL n;
scanf("%d",&T);
while(T--)
{
scanf("%I64d",&n);
if(Miller_Rabin(n))
{
printf("Prime\n");
continue;
}
tol=;
findfac(n);// 对n的素数分解
LL ans=factor[];
for(int i=;i<tol;i++)
if(factor[i]<ans)
ans=factor[i];
printf("%I64d\n",ans); for(int i=;i<tol;i++)
printf("%I64d ",factor[i]);
printf("\n");
}
return ;
}

poj 1811 Prime Test 大数素数测试+大数因子分解的更多相关文章

  1. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  2. Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test

    POJ 1811 Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 32534   Accepted: 8 ...

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

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

  4. 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 ...

  5. poj 3126 Prime Path( bfs + 素数)

    题目:http://poj.org/problem?id=3126 题意:给定两个四位数,求从前一个数变到后一个数最少需要几步,改变的原则是每次只能改变某一位上的一个数,而且每次改变得到的必须是一个素 ...

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

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

  7. POJ Pseudoprime numbers( Miller-Rabin素数测试 )

    链接:传送门 题意:题目给出费马小定理:Fermat's theorem states that for any prime number p and for any integer a > 1 ...

  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 素性测试 分解素因子

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

随机推荐

  1. vue二级路由跳转后外部引入js失效问题解决方案

    vue路由可以通过children嵌套,于是可以形成二级路由等等... 案例如下: routes: [ { path: '/', name: 'dy', component: dy, children ...

  2. 并发编程>>并发级别(二)

    理解并发 这是我在开发者头条看到的.@编程原理林振华 有目标的提升自己会事半功倍,前行的道路并不孤独. 1.阻塞 当一个线程进入临界区(公共资源区)后,其他线程必须在临界区外等待,待进去的线程执行完成 ...

  3. #Go# 点滴积累

    此篇仅为不断记录趟过的坑 StringToTimestamp import ( "time" ) const TimeFormat = "2006-01-02T15:04 ...

  4. 经典DP 洛谷p1880 石子合并

    https://www.luogu.org/problemnew/show/P1880 题目 题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新 ...

  5. vue-cli3 chainWebpack配置,去除打包后文件的预加载prefetch/preload(已解决)

    //细节配置修改 chainWebpack: config => { console.log(config,'chainWebpack') // 移除 prefetch 插件 config.pl ...

  6. dubbo和zookeeper的关系

    转载前言:网络上很多教程没有描述zookeeper和dubbo到底是什么关系.分别扮演了什么角色等信息,都是说一些似是而非的话,这里终于找到一篇文章,比较生动地描述了注册中心和微服务框架之间的关系,以 ...

  7. (转)MySQL高可用架构之MHA

    MySQL高可用架构之MHA  原文:http://www.cnblogs.com/gomysql/p/3675429.html 简介: MHA(Master High Availability)目前 ...

  8. PHPStorm操作小技巧

    1.围绕选中字符输入引号或者括号 2.设置服务器部署 3.隐藏Project快捷键 Shift + Esc 4.IDE内窗口切换 Ctrl + TAB 5.关闭当前项目 File -> Clos ...

  9. javascript中Function与Object

    1. 先来一段代码: console.log(Function); // function Function() { [native code] } console.log(Object); // f ...

  10. wap webapp app区别

    “手机WAP版网站”.“手机触屏版网站”.“手机APP应用软件”: wap webapp app区别 电脑版:台式机或者笔记本访问,兼容各个浏览器: Wap版:用于传统智能手机,屏幕小,适合使用手机键 ...