POJ-1811-Prime Test(pollard_rho模板,快速找最小素因子)

题目传送门

sol:Pollard_Rho的模板题,刚看了Pollard_Rho和Miller_Rabin很多原理性的东西看不懂,只是记住了结论勉强能敲代码。

  • Pollard_Rho
    #include "cstdio"
    #include "cstdlib"
    #include "algorithm"
    #include "ctime"
    using namespace std;
    typedef long long LL;
    LL gcd(LL a, LL b) {
        return b == 0 ? a : gcd(b, a % b);
    }
    LL muli_mod(LL n, LL k, LL p) {
        LL m = 0;
        while (k) {
            if (k & 1) m = (m + n) % p;
            n = (n + n) % p;
            k >>= 1;
        }
        return m;
    }
    LL pow_mod(LL n, LL k, LL p) {
        LL m = 1;
        while (k) {
            if (k & 1) m = muli_mod(m, n, p);
            n = muli_mod(n, n, p);
            k >>= 1;
        }
        return m;
    }
    LL miller_rabin(LL n) {
        if (n == 2) return true;
        if (n < 2 || !(n & 1)) return false;
        LL m = n - 1; int s = 0;
        while (!(m & 1)) s++, m >>= 1;
        for (int i = 1; i <= 5; i++) {
            LL r = rand() % (n - 1) + 1;
            LL y = pow_mod(r, m, n);
            for (int j = 1; j <= s; j++) {
                LL x = muli_mod(y, y, n);
                if (x == 1 && y != 1 && y != n - 1) return false;
                y = x;
            }
            if (y != 1) return false;
        }
        return true;
    }
    LL pollard_rho(LL n, LL c) {
        int i = 1, k = 2;
        LL x = rand() % (n - 1) + 1;
        LL y = x;
        while (true) {
            x = (muli_mod(x, x, n) + c) % n;
            LL p = gcd((y - x + n) % n, n);
            if (p > 1 && p < n) return p;
            if (x == y) return n;
            if (++i == k) {
                k <<= 1;
                y = x;
            }
        }
    }
    LL find(LL n) {
        if (miller_rabin(n)) return n;
        LL p = n;
        while (p >= n) p = pollard_rho(p, rand() % (n - 1) + 1);
        return min(find(p), find(n / p));
    }
    int main() {
        int t; LL n;
    //    srand(time(NULL));
        scanf("%d", &t);
        while (t--) {
            scanf("%lld", &n);
            LL p = find(n);
            if (p == n) puts("Prime");
            else printf("%lld\n", p);
        }
        return 0;
    }

    POJ不让用万能头,algorithm下的__gcd也不让用。关键srand用一下还RE,挺坑的。

转载于:https://www.cnblogs.com/Angel-Demon/p/11593930.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值