Pseudoprime numbers
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2704 Accepted Submission(s): 1159
Given 2 < p ≤ 1,000,000,000 and 1 < a < p, determine whether or not p is a base-a pseudoprime.
3 2 10 3 341 2 341 3 1105 2 1105 3 0 0
no no yes no yesyes
此题主要是判断素数和快速幂算法的应用,通过两个函数实现,自己没注意素数从2开始判断....
代码如下:
#include <cstdio> #include <algorithm> #include<stdint.h> #include<math.h> #include<string.h> __int64 quickpow(__int64 n,__int64 m,__int64 mod) { __int64 a=1,b=n; while(m) { if(m&1) { a=(b*a)%mod; } b=(b*b)%mod; m>>=1; } return a; } int su(__int64 a) { for(int i=2;i*i<a;i++) if(!(a%i)) return 1; return 0; } using namespace std; int main(){ __int64 p,a; while(~scanf("%I64d%I64d",&p,&a)&&p||a) { if(!su(p)) printf("no\n"); else { if(quickpow(a,p,p)==a) printf("yes\n"); else printf("no\n"); } } return 0; }