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

时间: 2024-08-08 15:09:14

POJ-1811-Prime Test(pollard_rho模板,快速找最小素因子)的相关文章

数论 - Miller_Rabin素数测试 + pollard_rho算法分解质因数 ---- poj 1811 : Prime Test

Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the

Miller&amp;&amp;Pollard POJ 1811 Prime Test

题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************************************************ * Author :Running_Time * Created Time :2015-8-28 13:02:38 * File Name :POJ_1811.cpp ************************************

poj 1811: Prime Test

题目链接 涉及到大数的素性判定及大数的质因数分解,感觉用处不大.当成一块模板好了 #include <cstdio> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; #define abs(a) (a)>=

POJ 1811 Prime Test(费马小定理+二次探测定理)

素数的测试: 费尔马小定理:如果p是一个素数,且0<a<p,则a^(p-1)%p=1. 利用费尔马小定理,对于给定的整数n,可以设计素数判定算法,通过 计算d=a^(n-1)%n来判断n的素性,当d!=1时,n肯定不是素数,当d=1时,n   很可能是素数. 二次探测定理:如果n是一个素数,且0<x<p,则方程x^2%p=1的解为:x=1或    x=p-1. 利用二次探测定理,可以再利用费尔马小定理计算a^(n-1)%n的过程 中增加对整数n的二次探测,一旦发现违背二次探测条件,

数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&amp;2429

素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: http://blog.csdn.net/maxichu/article/details/45459533 然后是参考了kuangbin的模板: http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646396.html 模板如下: //快速乘 (a

poj 1811, poj 2429 (pollard_rho算法)

poj 1811 题意: 给出一个整数n,判断n是不是素数,如果不是素数,输出最小的质因子. 限制: 2 <= N < 2^54 思路: miller_rabin算法判素数 pollard_rho算法求质因子 复杂度O(log(n)) poj 2429 题意: 给出两个数的lcm和gcd,求这两个数. 限制: 0 < lcm,gcd < 2^63 思路: pollard_rho O(log(n))分解质因数. 可以考虑到2^63不同的质因数只有20左右个,而相同的质数不可能分在不同

Prime Test POJ - 1811(素性检测+大数分解)

Prime Test POJ - 1811 题意:判断N (2 <= N < 2 54) 是不是素数,如果不是求它的最小素因数. millerRabin素性检测 + pollard rho大数分解 链接 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 #define LL l

poj 2888 Magic Bracelet(Polya+矩阵快速幂)

Magic Bracelet Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4990   Accepted: 1610 Description Ginny’s birthday is coming soon. Harry Potter is preparing a birthday present for his new girlfriend. The present is a magic bracelet which

poj 1781 In Danger(约瑟夫环,找规律)

http://poj.org/problem?id=1781 约瑟夫环的模板,每次数到2的人出圈. 但直接求会TLE,n太大. 打表发现答案和n有关系.当n是2的幂的时候,答案都是1,不是2的幂的时候都与小于2的幂那个数相差差值的2的倍数. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack&