uva 10006 Carmichael Numbers

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=947

打出素数表,快速幂取模。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 #define ll long long
 6 #define maxn 65000
 7 using namespace std;
 8 
 9 int n;
10 bool vis[maxn*10];
11 
12 
13 ll pow_mod(ll a,ll p,ll n)
14 {
15     if(p==0) return 1;
16     ll ans=pow_mod(a,p/2,n);
17     ans=ans*ans%n;
18     if(p%2==1) ans=ans*a%n;
19     return ans;
20 }
21 void Get_prime()
22 {
23     memset(vis,false,sizeof(vis));
24     int m=(int)sqrt(maxn*1.0);
25     for(int i=2; i<=m; i++)
26     {
27         if(!vis[i])
28         {
29             for(int j=i*i; j<=maxn; j+=i)
30             {
31                 vis[j]=true;
32             }
33         }
34     }
35 }
36 
37 
38 int main()
39 {
40     Get_prime();
41     while(scanf("%d",&n)!=EOF)
42     {
43         if(n==0) break;
44         if(!vis[n])
45         {
46             printf("%d is normal.
",n);
47             continue;
48         }
49         bool flag=true;
50         for(int i=2; i<n; i++)
51         {
52             ll c=pow_mod((ll)i,(ll)n,(ll)n);
53             if(c!=i)
54             {
55                 flag=false;
56                 break;
57             }
58         }
59         if(flag)
60         {
61             printf("The number %d is a Carmichael number.
",n);
62         }
63         else printf("%d is normal.
",n);
64     }
65     return 0;
66 }
View Code
原文地址:https://www.cnblogs.com/fanminghui/p/4013881.html