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. Unable to access 'default.path.data' (/var/lib/elasticsearch)

  2. solr安装教程

    Solr Solr is the popular, blazing-fast, open source enterprise search platform built on Apache Lucen ...

  3. day05 --class --home

    # -*- coding: utf-8 -*-# @Time : 2018/12/25 14:24# @Author : Endless-cloud# @Site : # @File : day05 ...

  4. 定期删除Azure存储账号下N天之前的数据文件-ASM

    ######RemoveStorageBlob*DaysOld##### <# .SYNOPSIS Remove all blob contents from one storage accou ...

  5. 皕杰报表 javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

    今天做报表的时候,跳转到显示报表页面的时候不出来数据,报错说数据集未产生. 后台报错 javax.naming.NameNotFoundException: Name jdbc is not boun ...

  6. c#中异步编程

    异步是现实生活中的很多现象的一种抽象.比如分工合作在很多时间段就是异步合作.异步中也一般要涉及委托方法.c#有3种模式的异步编程:异步模式,基于事件的异步模式,基于任务的异步模式(TAP). 一.   ...

  7. OpenERP how to set the tree view limit

    return { 'name':u'库存报表', 'view_type':'form', 'view_mode':'tree,form', 'res_model':'rainsoft.account. ...

  8. OpenERP 疑问之一

    def _get_send_amount(self,cr,uid,ids,name,args,context=None): res={} MRP={} lines = self.browse(cr,u ...

  9. Python中utf-8与utf-8-sig两种编码格式的区别

    As UTF-8 is an 8-bit encoding no BOM is required and anyU+FEFF character in the decoded Unicode stri ...

  10. EasyMock set方法报错: java.lang.AssertionError

    有返回值的方法没问题, 直接andReturn就行了. EasyMock.expect(info.getWebTitle()).andReturn(StringUtils.EMPTY).anyTime ...