poj 1811 Prime Test(大素数判定)
2014-04-15 12:36
417 查看
Prime Test
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
Sample Output
Source
POJ Monthly
题意:判断一个数是否素数,否的话输出其最小的因子
题解:用miller大素数判定,和pollar求因子
Time Limit: 6000MS | Memory Limit: 65536K | |
Total Submissions: 28142 | Accepted: 7044 | |
Case Time Limit: 4000MS |
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
题意:判断一个数是否素数,否的话输出其最小的因子
题解:用miller大素数判定,和pollar求因子
#include<iostream> #include<cstdlib> #include<ctime> #include<cmath> #define TIME 8 #define C 50 #define MAX (pow(2.0,60)) using namespace std; long long MIN; long long gcd(long long a,long long b) { if(!b) return a; return gcd(b,a%b); } long long mod_mult(long long a,long long b,long long mod) { long long s=0; a=a%mod; while(b) { if(b&1) { s+=a; if(s>=mod) s-=mod; } a=a<<1; if(a>=mod) a-=mod; b=b>>1; } return s; } long long mod_pow(long long a,long long b,long long mod) { long long d=1; a=a%mod; while(b) { if(b&1) d=mod_mult(d,a,mod); a=mod_mult(a,a,mod); b=b>>1; } return d; } bool wintess(long long a,long long n) { long long m=n-1,x,y; int i,j=0; while(m%2==0) m>>=1,j++; x=mod_pow(a,m,n); for(i=1; i<=j; i++) { y=mod_pow(x,2,n); if((y==1)&&(x!=1)&&(x!=n-1)) return true; x=y; } if(y!=1) return true; return false; } bool miller_rabin(int times,long long n) { long long a; if(n==1) return false; if(n==2) return true; if(n%2==0) return false; srand(time(NULL)); for(int i = 1 ; i<=times; i++) { a=rand()%(n-1)+1; if(wintess(a,n)) return false; } return true; } long long pollard(long long n,int c) { long long i=1,k=2,x,y,d,tot=1<<15; srand(time(NULL)); y=x=rand()%n; while(true) { i++; x=(mod_mult(x,x,n)+c)%n; d=gcd(y-x,n); if(d>1&&d<n) return d; if(y==x) return n; if(i==k) { y=x; k=k<<1; } tot--; if(!tot) return n; } } void get_small(long long n,int c) { long long m; if(n==1) return; if(miller_rabin(TIME,n)) { if(n<MIN) MIN=n; return; } m=n; while(m==n) m=pollard(n,c--); get_small(m,c); get_small(n/m,c); } int main() { long long n; int t; ios::sync_with_stdio(false); cin >> t; while(t--) { cin >> n; if(n==2||miller_rabin(TIME,n)) cout << "Prime\n"; else if(n%2==0) { cout << "2\n"; } else { MIN=MAX; get_small(n,C); cout << MIN << endl; } } return 0; }
相关文章推荐
- 项目记录14.4.15
- Go语言test之类方法测试
- 开源分布式搜索平台ELK(Elasticsearch+Logstash+Kibana)入门学习资源索引
- 微软通过.NET Native为Windows Store应用提速
- linux 服务器网络有关的内核参数
- linux 服务器网络有关的内核参数
- linux 服务器网络有关的内核参数
- windows之查看字符集
- shared_ptr(共享指针)使用总结
- hdu 1312 Red and Black
- .NET基金会成立
- 产品经理要有特别的沟通技巧
- tomcat 原理(一)
- 程序员的思想
- 计算2+4+6+...+100
- hive异常 show tables 无法使用 : Unable to instantiate rg.apache.hadoop.hive.metastore.HiveMetaStoreClient
- buntu的ip设置
- 要学车暂停更新几个星期
- idf inverse document frequency
- JNI的替代者—使用JNA访问Java外部函数接口