UVaOJ10006 - Carmichael Numbers

10006 - Carmichael Numbers

Time limit: 3.000 seconds

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, nis probably prime if the following equation holds: 

\begin{displaymath}a^n \bmod n = a\end{displaymath}

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.



Miguel Revilla 
2000-08-21

题目大意:

            判断一个数是否满足: 1 、 是合数    2、对于任意的2 <= a <= n - 1 是否满足

 \begin{displaymath}a^n \bmod n = a\end{displaymath}

解题方法:

     筛选法得素数表

     (ab) mod c = ((a mod c) * (b mod c)) mod c.    利用这个性质,二分法快速幂取模。

源代码:

#include <cstdio>
#include <cstring>
#include <cstring>
#include <cmath>

const int MAXN = 65010;
bool prime[MAXN];

void init() {
    memset(prime, true, sizeof(prime));

    for (int i = 2; i <= (int)sqrt(MAXN*1.0); ++i)
        if (prime[i])
            for (int j = i * i; j < MAXN; j += i)
                prime[j] = false;
}

long long int powmod(int a, int n, int m) {
    if (n == 1)
        return a % m;

    long long int res;
    res = powmod(a, n >> 1, m);
    res = (res * res) % m;

    if (n % 2)
        return (a * res) % m;
    else
        return res;
}

bool judge(int n) {
    if (prime[n])
        return false;

    for (int i = 2; i < n; ++i)
        if (powmod(i, n, n) != i)
            return false;
    return true;
}

int main() {
    int n;
    init();
    while (scanf("%d", &n) && n) {
        if (judge(n))
            printf("The number %d is a Carmichael number.\n", n);
        else
            printf("%d is normal.\n", n);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Carmichael定理是一个与费马小定理相关的定理,它给出了一种更准确地判断一个数是否为素数的方法。Carmichael定理指出,如果一个数n是素数,那么对于任意整数a,满足a与n互质,即gcd(a,n)=1,都有a^(λ(n)) ≡ 1 (mod n),其中λ(n)是n的Carmichael函数。Carmichael函数λ(n)是欧拉函数φ(n)的一个特殊情况,它表示与n互质的整数的最小指数,使得a^λ(n) ≡ 1 (mod n)成立。 Carmichael定理的应用是在判断一个数是否为素数时,通过验证a^(n-1) ≡ 1 (mod n)对于一定数量的随机选择的a是否成立,可以更准确地判断一个数是否为素数。这是因为Carmichael数存在的情况下,费马小定理可能会误判一个合数为素数,而Carmichael定理可以避免这种情况的发生。 总结来说,Carmichael定理是一个用于判断一个数是否为素数的定理,它通过验证a^(λ(n)) ≡ 1 (mod n)对于一定数量的随机选择的a是否成立,可以更准确地判断一个数是否为素数。\[1\]\[3\] #### 引用[.reference_title] - *1* *2* [费马小定理及其应用](https://blog.csdn.net/WYW1996/article/details/102046924)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Carmichael function[卡迈克尔函数相关性质]](https://blog.csdn.net/AdijeShen/article/details/108476229)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值