zoukankan      html  css  js  c++  java
  • 米勒罗宾素数测试法

    #include<iostream>
    #include<cstdio>
    using namespace std;
    typedef long long LL;
    // 18位素数:154590409516822759
    // 19位素数:2305843009213693951 (梅森素数)
    // 19位素数:4384957924686954497
    LL prime[6] = {2, 3, 5, 233, 331};
    LL qmul(LL x, LL y, LL mod) { // 乘法防止溢出, 如果p * p不爆LL的话可以直接乘; O(1)乘法或者转化成二进制加法
    
    
        return (x * y - (long long)(x / (long double)mod * y + 1e-3) *mod + mod) % mod;
        /*
        LL ret = 0;
        while(y) {
            if(y & 1)
                ret = (ret + x) % mod;
            x = x * 2 % mod;
            y >>= 1;
        }
        return ret;
        */
    }
    LL qpow(LL a, LL n, LL mod) {
        LL ret = 1;
        while(n) {
            if(n & 1) ret = qmul(ret, a, mod);
            a = qmul(a, a, mod);
            n >>= 1;
        }
        return ret;
    }
    bool Miller_Rabin(LL p) {
        if(p < 2) return 0;
        if(p != 2 && p % 2 == 0) return 0;
        LL s = p - 1;
        while(! (s & 1)) s >>= 1;
        for(int i = 0; i < 5; ++i) {
            if(p == prime[i]) return 1;
            LL t = s, m = qpow(prime[i], s, p);
            while(t != p - 1 && m != 1 && m != p - 1) {
                m = qmul(m, m, p);
                t <<= 1;
            }
            if(m != p - 1 && !(t & 1)) return 0;
        }
        return 1;
    }
    int main()
    {
        LL n;
        while(cin>>n)
        {
            if(Miller_Rabin(n))
                cout<<true<<endl;
            else cout<<false<<endl;
        }
        return 0;
    }
  • 相关阅读:
    马肯9450命令回传
    上海公积金社保业务办理
    工厂调试命令总结
    数据库自动备份
    powerDesigner16.5导出word
    winform 打印预览
    winform 页面设计(一)
    梅特勒-托利多 TCS-35 电子台秤
    winform 练习多线程
    ASP.NET Core 3.1使用Swagger API接口文档
  • 原文地址:https://www.cnblogs.com/myxdashuaige/p/8975898.html
Copyright © 2011-2022 走看看