首页 技术 正文
技术 2022年11月16日
0 收藏 489 点赞 2,760 浏览 1938 个字

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

POJ Monthly【分析】模板题

 /*
宋代谢逸
《踏莎行·柳絮风轻》
柳絮风轻,梨花雨细。春阴院落帘垂地。碧溪影里小桥横,青帘市上孤烟起。
镜约关情,琴心破睡。轻寒漠漠侵鸳被。酒醒霞散脸边红,梦回山蹙眉间翠。
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#define LOCAL
const int MAXN = + ;
using namespace std;
typedef long long ll;
ll n, Ans; //快速乘
long long multi(long long a, long long b, long long c){
if (b == ) return ;
if (b == ) return a % c;
long long tmp = multi(a, b / , c);
if (b % == ) return (tmp + tmp) % c;
else return (((tmp + tmp) % c) + a) % c;
}
ll pow(ll a, ll b, ll p){
if (b == ) return a % p;
ll tmp = pow(a, b / , p);
if (b % == ) return (multi(tmp, tmp, p));
else return multi(multi(tmp, tmp, p), (a % p), p);
}
//二次探测
bool Sec_Check(ll a, ll p, ll c){
ll tmp = pow(a, p, c);
if (tmp != && tmp != (c - )) return ;//不通过
if (tmp == (c - ) || (p % != )) return ;
return Sec_Check(a, p / , c);
}
bool miller_rabin(ll n){
ll cnt = ;
while (cnt--){
ll a = (rand()%(n - )) + ;
if (!Sec_Check(a, n - , n)) return ;
}
return ;
}
//int f(int ) {return }
long long gcd(long long a, long long b){return b == ? a : gcd(b, a % b);}
long long BIGRAND() {return rand() * RAND_MAX + rand();}
long long pollard_rho(long long n, long long c){
long long x, y, d;
long long i = , k = ;
x = ((double)rand()/RAND_MAX*(n - )+0.5) + ;
y = x;
while(){
i++;
//注意顺序
x = (multi(x, x, n) % n + c) % n;
d = gcd(y - x + n, n);
if( < d && d < n) return d;
if(y == x) return n;
if(i == k){
y = x;
k <<= ;
}
}
}
//
void find(long long n, long long c){
if (n == ) return;
if (miller_rabin(n)) {
if (Ans == -) Ans = n;
else Ans = min(Ans, n);
return ;
}
long long p = n;
while (p >= n) p = pollard_rho(n, c--);
find(p, c);
find(n / p, c);
//return find(p, c) + find(n / p, c);
} int main(){
int T;
srand(time()); scanf("%d", &T);
while (T--){
scanf("%lld", &n);
if (n != && miller_rabin(n)) printf("Prime\n");
else {
Ans = -;
find(n, );
printf("%lld\n", Ans);
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898