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. LOJ#2039. 「SHOI2015」激光发生器(计算几何)

    题面 传送门 题解 如果我初中科学老师知道我有一天计算的时候入射角不等于反射角不知道会不会把我抓起来打一顿-- 这题本质上就是个模拟,需要的芝士也就计蒜几盒的那点,不过注意细节很多,放到考场上只能看看 ...

  2. python反转列表的几种方法

    一.使用reversed()函数 a = [1, 2, 3, 4] b = list(reversed(a)) 注意:reversed()函数返回的是一个迭代器,而不是一个List,需要再使用List ...

  3. Elasticsearch(八)【NEST高级客户端--分析器】

    分析 分析是将文本(如任何电子邮件的正文)转换为添加到反向索引中进行搜索的tokens或terms的过程. 分析由analyzer执行,分析器可以是内置分析器或每个索引定义的定制分析器. 书写分析器测 ...

  4. POJ1095 Trees Made to Order(JAVA)

    这题用到了卡特兰数,比较麻烦.关于卡特兰数的基本概念百度一下你就知道. 使用卡特兰数对数组元素进行分组之后,需要具体计算一下要求的是第几组的第几个数,然后向下递归. 首先来看利用卡特兰数分组: 从1开 ...

  5. 2016级算法期末上机-G.中等·Bamboo's Fight with DDLs II

    中等·Bamboo's Fight with DDLs II 分析 一句话:给定字符串,求最长回文子序列长度,动态规划LCS思想的进阶应用 具体思路如下: 对于任意字符串,如果头尾字符相同,那么字符串 ...

  6. 2016级算法第六次上机-D.AlvinZH的学霸养成记V

    1081 AlvinZH的学霸养成记V 思路 中等题,计算几何. 这是一个排序问题,按极角排序.可以转化为叉积的应用,对于点A和B,通过叉积可以判断角度大小,共线时再判断距离. 叉积的应用.OA × ...

  7. SuperMap(无对应字段)空间属性挂接

    一.数据准备 将一个没有基本属性的模型数据集和一个含有位置等属性信息的shp属性表进行挂接,两组数据之间没有连接字段,但同属一个坐标系下,只能通过空间位置信息进行属性挂接. 二.数据处理 基本思路:将 ...

  8. LARTC

    大牛的博客 howto ,however, is simplify. another space ip link list ip address show ip route show route -n ...

  9. python 进程与线程(理论部分)

    一.理论部分 一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的 ...

  10. excel 快速填充所有非连续空白单元格

    工作中经常会碰到这样的表,需要把空白的单元格填充完成.变成后图.下面就是方法.       工具/原料 EXCEL 方法/步骤   首先选中你要填充的区域,   按“F5"或者 Ctrl + ...