uva 10006 费马测试不符合的数


能通过费马测试的但这个数不是素数的数叫Carmichael numbers,给你一些数,让你判断这个数是不是Carmichael numbers。

n比较小,直接从2到n-1一个个测试就行。求a的n次方用快速幂运算。另外自己写的快速幂运算函数最好不要跟系统的函数重名,

因为有时候参数类型不一致的时候它会调用系统的函数。。我就这里错了很多次但始终找不出原因。

#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;

inline int in()
{
    int res=0;
    char c;
    while((c=getchar())<'0' || c>'9');
    while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
    return res;
}
const int N=66000;
bitset<N> vis;

ll power(ll a,ll n)
{
    ll res=1;
    ll mod=n;
    while(n)
    {
        if(n&1)res=res*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return res;
}

int main()
{
    for(int i=2;i<N;i++)
    {
        if(!vis[i])
        {
            for(int j=i+i;j<N;j+=i)vis[j]=1;
        }
    }

    int n;
    while(~scanf("%d",&n) && n)
    {
        if(!vis[n])
        {
            printf("%d is normal.\n",n);
            continue;
        }
        bool ok=1;
        for(int i=2;i<=n-1 && ok;i++)
        {
            if(i!=power(i,n))ok=0;
        }

        if(ok)
            printf("The number %d is a Carmichael number.\n",n);
        else
            printf("%d is normal.\n",n);
        
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值