Uva 10006 Carmichael Numbers(数论、快速幂、素数筛法)
题目
Time Limit:3000MS Memory Limit:0KB
Description
An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography.
Alvaro is one of such persons, and is designing a set of cryptographic procedures for cooking paella. ´ Some of the cryptographic algorithms he is implementing make use of big prime numbers. However, checking if a big number is prime is not so easy. An exhaustive approach can require the division of the number by all the prime numbers smaller or equal than its square root. For big numbers, the amount of time and storage needed for such operations would certainly ruin the paella.
However, some probabilistic tests exist that offer high confidence at low cost. One of them is the Fermat test. Let a be a random number between 2 and n−1 (being n the number whose primality we are testing). Then, n is probably prime if the following equation holds:
an mod n = a
If a number passes the Fermat test several times then it is prime with a high probability.
Unfortunately, there are bad news. Some numbers that are not prime still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers.
In this problem you are asked to write a program to test if a given number is a Carmichael number. Hopefully, the teams that fulfill the task will one day be able to taste a delicious portion of encrypted paella. As a side note, we need to mention that, according to Alvaro, the main advantage of encrypted paella over conventional paella is that nobody but you knows what you are eating.
Input
The input will consist of a series of lines, each containing a small positive number n (2 < n < 65000).
A number n = 0 will mark the end of the input, and must not be processed.
Output
For each number in the input, you have to print if it is a Carmichael number or not, as shown in the sample 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.
题意
给你一个数n,当n不是素数且从2~n-1存在数满足 an mod n = a时,此数为Carmichael number。
分析
1.对于是否为素数,暴力打表会TLE,使用素数筛即可。
2.判断
an
,使用快速幂
注意:要在开始即判断n是否为素数,如果运算 an 后才开始判断,会T。
源码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<sstream>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<utility>
#include<sstream>
#define mem0(x) memset(x,0,sizeof x)
#define mem1(x) memset(x,-1,sizeof x)
#define dbug cout<<"here"<<endl;
//#define LOCAL
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MAXN = 65001;
const int MOD = 1000000007;
bool is_prime[65010];
ull quickMod(ull a, ull b,ull mod){
ull ans = 1;
a %= mod;
while(b){
if(b & 1){
ans = (ans*a)%mod;
}
b = b >> 1;
a = (a*a)%mod;
}
return ans;
}
void sieve(){
memset(is_prime, true, sizeof is_prime);
is_prime[0] = is_prime[1] = true;
for(int i = 2; i <= sqrt(MAXN); ++i){
if(is_prime[i]){
for(int j = i+i; j <= MAXN; j+=i){
is_prime[j] = false;
}
}
}
}
int main(){
#ifdef LOCAL
freopen("C:\\Users\\asus-z\\Desktop\\input.txt","r",stdin);
freopen("C:\\Users\\asus-z\\Desktop\\output.txt","w",stdout);
#endif
int n;
sieve();
while(cin >> n,n){
if(is_prime[n]){
cout << n << " is normal." << endl;
}
else{
for(int i = 2; i < n; ++i){
if(quickMod(i, n, n) != i){
cout << n << " is normal." << endl;
goto here;
}
}
printf("The number %d is a Carmichael number.\n",n);
goto here;
}
here:;
}
return 0;
}

本文介绍了一种用于检测Carmichael数的算法,并通过快速幂运算验证了这些数是否符合特定条件。Carmichael数是一类特殊的合数,它们在Fermat素性测试中表现出类似素数的行为。文章提供了完整的源代码实现,并使用了素数筛法来提高效率。
506

被折叠的 条评论
为什么被折叠?



