微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

POJ-1811-Prime Testpollard_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 p) {
        LL m = 1;
        while (k) {
            if (k & 1) m = muli_mod(m,n,p);
            n = muli_mod(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,挺坑的。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐