题意:
给你两个数,p和a;满足两个条件:
1.p不是素数;
2.apa^{p}ap %p==a;
满足则输出yes,反之输出no。
题目:
Fermat’s theorem states that for any prime number p and for any integer a > 1, apa^{p}ap = a (mod p). That is, if we raise a to the pth power and divide by p, the remainder is a. Some (but not very many) non-prime values of p, known as base-a pseudoprimes, have this property for some a. (And some, known as Carmichael Numbers, are base-a pseudoprimes for all a.)
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 containing p and a.
Output
For each test case, output “yes” if p is a base-a pseudoprime; otherwise output “no”.
Sample Input
3 2
10 3
341 2
341 3
1105 2
1105 3
0 0
Sample Output
no
no
yes
no
yes
yes
分析:
这道题一旦题意出来了,其他就是常规操作,第一个条件p为非素数很容易看出,但第二个条件,恕我眼拙,题目给了一个【a > 1, apa^{p}ap = a (mod p). 】的条件,我很容易就想到了费马小定理的xp−1≡1(modp)x^{p-1}\equiv 1(mod p)xp−1≡1(modp),然后就掉进坑里爬都爬不起来,看到博客上的题意,心情很崩溃。
AC代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
ll p,a;
bool prime(ll x){int k=0;for(int i=2;i*i<=x;i++)if(x%i==0)k++;if(k==0) return true;return false;
}
ll mod(ll a,ll p,ll mod){ll ans=1;a%=mod;while(p){if(p&1) ans=a*ans%mod;p>>=1;a=a*a%mod;}return ans;
}
int main(){while(~scanf("%lld%lld",&p,&a)){if(!p&&!a)break;if(prime(p)){printf("no\n");continue;}if(mod(a,p,p)==a) printf("yes\n");else printf("no\n");}return 0;
}