POJ 3641 质数+快速幂

来源:互联网 发布:淘宝芊芊香港代购 编辑:程序博客网 时间:2024/05/23 19:36
Pseudoprime numbers
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11026 Accepted: 4756

Description

Fermat's theorem states that for any prime number p and for any integera > 1, ap = a (mod p). That is, if we raise a to thepth power and divide by p, the remainder is a. Some (but not very many) non-prime values ofp, known as base-a pseudoprimes, have this property for somea. (And some, known as Carmichael Numbers, are base-a pseudoprimes for alla.)

Given 2 < p ≤ 1000000000 and 1 < a < p, determine whether or not p is a base-a pseudoprime.

Input

Input contains several test cases followed by a line containing "0 0". Each test case consists of a line containingp and a.

Output

For each test case, output "yes" if p is a base-a pseudoprime; otherwise output "no".

Sample Input

3 210 3341 2341 31105 21105 30 0

Sample Output

nonoyesnoyesyes
题意是给你两个数p,a,如果 a的p次方对p求余得到的余数等于a,并且p不是质数就输出yes 否则输出no
这道题本来觉得测试组数会挺多的 2 < p ≤ 1000000000,于是想到打质数表,一次将所有质数搞定,结果出现了超内存的情况,后来循规蹈矩的求了一遍素数,结果没想到过了。
#include<iostream>#include<stdio.h> using namespace std;//long long a[10000000] = {0,0,1};long long quickpow(long long a, long long b,long long c){long long ans = 1; while(b){if(b&1)ans = (a*ans) %c;//更新多出来的幂底数b = b>>1;a = (a*a)%c;}return ans;}int main(){long long p,b;/*for(long long i = 3 ; i <= 10000000 ; i += 2){ //质数表打表,质数标志为1,不是为0a[i+1] = 0; //二的倍数除了二,其他都非质数,三的倍数,除了三,其他都是非质数a[i] = 1;}for(long long i = 3 ; i <= 10000000 ; i ++){for(long long j = i+i ; j <= 10000000 ; j+=i){a[j] = 0;}}*/while(scanf("%lld%lld",&p,&b)){if(!(p&&b))break;int sum = 0 ;for(int i=2;i*i<p;i++)          {              if(p%i==0)              sum++;          }  if(b == quickpow(b,p,p)&&(sum))cout<<"yes"<<endl;elsecout<<"no"<<endl; }}

原创粉丝点击