Uva 10006 Carmichael Numbers 快速幂

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.

题意:给定这样的一个定义:给你一个数n,对于任意的x(1< x < n),都有x^n ≡x(mod n)。那么就称这个数Carmichael number。如果不是,则输出”is normal“。

题解:对于一个数n的判断次数就是2~n-1。如果不处理幂运算,那么肯定是会超时得。我们可以采用快速幂的算法。要注意在进行快速幂之前先判断一次n是否为素数,如果是素数那么就is normal 并且continue了。不进行这样的操作很有可能超时。

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<queue>
#include<cmath>
using namespace std;
long long int mod_pow(long long int x,long long int n,long long int mod)
{
    long long int ret=1;
    while(n>0)
    {
        if(n&1)
            ret=ret*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return ret;
}
bool prime(int x)
{
    for(int i=2;i<=sqrt(x);i++)
    {
        if(x%i==0)
        return false;
    }
    return true;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0)
            break;
        bool flag=true;
        if(prime(n))
        {
             printf("%d is normal.\n",n);
             continue;
        }
        for(int i=2;i<n;i++)
        {
            if(mod_pow(i,n,n)!=i)
            {
                flag=false;
                break;
            }
        }
        if(!prime(n)&&flag)
            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、付费专栏及课程。

余额充值