Codevs1702 素数判定2

题目描述 Description
一个数,他是素数么?
设他为P满足(P<=2^63-1)

输入描述 Input Description
P

输出描述 Output Description
Yes|No

样例输入 Sample Input
2

样例输出 Sample Output
Yes

数据范围及提示 Data Size & Hint
算法导论——数论那一节
注意Carmichael Number

代码 Code

//来自Solution_ID:14059
#include<cstdio>
#include<iostream>
typedef long long LL;
using namespace std;

const int primesize = 8;
const int prime[] = {2,3,5,7,11,13,17,19};

LL mul(LL a,LL b,LL m){
    return (a*b-(LL)(a/(long double)m*b+1e-3)*m+m)%m;
}

LL ksm(LL a,LL k,LL m){
    if(k == 1) return a%m;
    if(!k) return 1;
    LL b = 1;
    while(k){
        if(k&1) b = mul(a,b,m);
        a = mul(a,a,m);
        k /= 2;
    }
    return b;
}

bool MR(LL x,LL a){
    if(x == a) return true;
    LL m = x-1;
    while(!(m%2)) m /= 2;
    LL mi = ksm(a,m,x);
    while(m != x-1 && mi != 1 && mi != x-1)
        mi = mul(mi,mi,x),m *= 2;
    return (m&1 || mi == x-1);
} 

bool Isprime(LL x){
    if(x == 2) return true;
    if(x < 2 || x % 2 == 0) return false;
    for(int i = 0;i < primesize;i++){
        if(!MR(x,prime[i]))
            return false;
    }
    return true;
}

int main(){
    LL x;
    scanf("%lld",&x);
    if(Isprime(x)) printf("Yes");
    else printf("No");
    return 0;
}

分析
其实是看完题解打的(凭良心说)

理论基础
MillerRabbin素数判定+二次探测原理

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值