Pseudoprime numbers(POJ 3641 快速幂)

 1 #include <cstring>
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cmath>
 5 #include <algorithm>
 6 using namespace std;
 7 #define LL long long
 8 LL p,res,a;
 9 bool judge_prime(LL k)
10 {
11     LL i;
12     LL u=int(sqrt(k*1.0));
13     for(i=2;i<=u;i++)
14     {
15         if(k%i==0)
16             return 0;
17     }
18     return 1;
19 }
20 int main()
21 {
22     freopen("in.txt","r",stdin);
23     while(scanf("%lld%lld",&p,&a))
24     {
25         if(p==0&&a==0)
26             break;
27         if(judge_prime(p))
28         {
29             printf("no
");
30             continue;
31         }
32         res=1;
33         LL t=p;
34         LL w=a;
35         while(p)
36         {
37             if(p&1) res=(res*a)%t;
38             a=(a*a)%t;
39             p>>=1;
40         }
41         if(res==w)
42             printf("yes
");
43         else
44             printf("no
");
45     }
46 }
原文地址:https://www.cnblogs.com/a1225234/p/5190495.html