加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 05:06:19 所属栏目:大数据 来源:网络整理
导读:题目传送门 sol: Pollard_Rho的模板题,刚看了Pollard_Rho和Miller_Rabin很多原理性的东西看不懂,只是记住了结论勉强能敲代码。 Pollard_Rho #include " cstdio " #include " cstdlib " #include " algorithm " #include " ctime " using namespace std;typ

题目传送门

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("%lldn",p);
        }
        return 0;
    }

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读