UVA 10006(素数打表+快速幂)

当今计算机科学的一个重要的领域就是密码学。有些人甚至认为密码学是计算机科学中唯一重要的领域,没有密码学生命都没有意义。

  阿尔瓦罗就是这样的一个人,它正在设计一个为西班牙杂烩菜饭加密的步骤。他在加密算法中应用了一些非常大的素数。然而确认一个非常大的数是不是素数并不是那么简单。一个费时的方法是用比这个数的平方根小的所有素数去除它,对于大整数来说,这样一定会毁掉这个杂烩菜饭的。
  然而,一些很有信心耗时少的随机测试存在,其中一个就是费马测试。
  在2和n-1之间随机选取一个数(n是我们要测试的数)。如果a n mod n = a 成立,n就可能是一个素数。
  如果一个数通过费马测试很多次那么它就很可能是一个素数。
  不幸的是,一些数不是素数但是它们依然能通过每一个比它小的数的费马测试。这些数被称作卡迈克尔数
  这道题要求你写一个程序去测试给定的数是不是一个卡迈克尔数。
  完成了这个任务的队伍有希望接受来自阿尔瓦罗的西班牙杂烩菜饭23333

Input
多组输入,第一行给一个n (2 < n < 65000) 。n = 0 表示输入结束并不需要处理
Output

对每组输入,输出它是不是卡迈克尔数,参考样例。

Sample Input
1729
17
561
1109
431
0
Sample Output
The number 1729 is a Carmichael number.
17 is normal.
The number 561 is a Carmichael number.
1109 is normal.
431 is normal.
题目是别人翻译过来的
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<map>
 8 #include<set>
 9 #include<vector>
10 #include<cstdlib>
11 #include<string>
12 #define eps 0.000000001
13 typedef long long ll;
14 typedef unsigned long long LL;
15 using namespace std;
16 const int N=1000000;
17 int  prime[N];
18 void _prime(){
19     for(int i=2;i*i<=N;i++){
20         if(prime[i]==0){
21             for(int j=i*i;j<=N;j=j+i){
22                 prime[j]=1;
23             }
24         }
25     }
26 }
27 ll kuaishumi(ll a,ll b){
28     ll ans=1;
29     ll mod=b;
30     while(b){
31         if(b&1){
32             ans=ans*a%mod;
33         }
34         a=a*a%mod;
35         b=b/2;
36     }
37     return ans;
38 }
39 int main(){
40     int a,n;
41     _prime();
42     while(scanf("%d",&a)!=EOF){
43         if(a==0)break;
44         if(prime[a]==0){
45             printf("%d is normal.
",a);
46            // cout<<1<<endl;
47             continue;
48         }
49         int flag=1;
50         for(int i=2;i<a;i++){
51             if(kuaishumi(i,a)!=i){
52                 flag=0;
53                 break;
54             }
55 
56         }
57         if(flag==1)
58             printf("The number %d is a Carmichael number.
",a);
59         else
60             printf("%d is normal.
",a);
61 
62     }
63 }
原文地址:https://www.cnblogs.com/Aa1039510121/p/6405508.html