题目传送门

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 == ? a : gcd(b, a % b);
    }
    LL muli_mod(LL n, LL k, LL p) {
    LL m = ;
    while (k) {
    if (k & ) m = (m + n) % p;
    n = (n + n) % p;
    k >>= ;
    }
    return m;
    }
    LL pow_mod(LL n, LL k, LL p) {
    LL m = ;
    while (k) {
    if (k & ) m = muli_mod(m, n, p);
    n = muli_mod(n, n, p);
    k >>= ;
    }
    return m;
    }
    LL miller_rabin(LL n) {
    if (n == ) return true;
    if (n < || !(n & )) return false;
    LL m = n - ; int s = ;
    while (!(m & )) s++, m >>= ;
    for (int i = ; i <= ; i++) {
    LL r = rand() % (n - ) + ;
    LL y = pow_mod(r, m, n);
    for (int j = ; j <= s; j++) {
    LL x = muli_mod(y, y, n);
    if (x == && y != && y != n - ) return false;
    y = x;
    }
    if (y != ) return false;
    }
    return true;
    }
    LL pollard_rho(LL n, LL c) {
    int i = , k = ;
    LL x = rand() % (n - ) + ;
    LL y = x;
    while (true) {
    x = (muli_mod(x, x, n) + c) % n;
    LL p = gcd((y - x + n) % n, n);
    if (p > && p < n) return p;
    if (x == y) return n;
    if (++i == k) {
    k <<= ;
    y = x;
    }
    }
    }
    LL find(LL n) {
    if (miller_rabin(n)) return n;
    LL p = n;
    while (p >= n) p = pollard_rho(p, rand() % (n - ) + );
    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 ;
    }

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

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

  1. Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test

    POJ 1811 Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 32534   Accepted: 8 ...

  2. POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)

    题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...

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

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 29046   Accepted: 7342 Case ...

  4. Miller&&Pollard POJ 1811 Prime Test

    题目传送门 题意:素性测试和大整数分解, N (2 <= N < 254). 分析:没啥好讲的,套个模板,POJ上C++提交 收获:写完这题得到模板 代码: /************** ...

  5. POJ 1811 Prime Test (Pollard rho 大整数分解)

    题意:给出一个N,若N为素数,输出Prime.若为合数,输出最小的素因子.思路:Pollard rho大整数分解,模板题 #include <iostream> #include < ...

  6. POJ 1811 Prime Test

    题意:对于一个大整数,判断是否质数,如果不是质数输出最小质因子. 解法:判断质数使用Miller-Rabin测试,分解质因子使用Pollard-Rho,Miller-Rabin测试用的红书模板,将测试 ...

  7. poj 1811 Prime Test 大数素数测试+大数因子分解

    Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 27129   Accepted: 6713 Case ...

  8. POJ 1811 Prime Test( Pollard-rho整数分解经典题 )

    链接:传送门 题意:输入 n ,判断 n 是否为素数,如果是合数输出 n 的最素因子 思路:Pollard-rho经典题 /************************************** ...

  9. POJ 1811 Prime Test(Miller-Rabin & Pollard-rho素数测试)

    Description Given a big integer number, you are required to find out whether it's a prime number. In ...

随机推荐

  1. F - Moving Points树状数组

    题:https://codeforces.com/contest/1311/problem/F 题意:给定x轴上的点以及他们的速度v,只在x轴上运动,求最小的dis之和,注意,这里的时间是可随意的,比 ...

  2. 排序算法 python实现

    一.排序的基本概念和分类 所谓排序,就是使一串记录,按照其中的某个或某些关键字的大小,递增或递减的排列起来的操作.排序算法,就是如何使得记录按照要求排列的方法. 排序的稳定性: 经过某种排序后,如果两 ...

  3. 查路由途径 traceroute tracert

    linux 用  traceroute IP windows用 tracert IP 虚拟机下使用无效

  4. awk grep sed 的一些问题

    条件   匹配    打印含关键字的行 ps aux  | sort -k 4 -r | awk '$4 ~ /^[0-9]/ && $4>0 {print $4,$11}' z ...

  5. 利用Matlab神经网络计算包预测近四天除湖北外新增确诊人数:拐点已现

    数据来源: 国家卫健委 已经7连降咯! 1.20-2.10图示(更新中): 神经网络训练并预测数据: clear %除湖北以外全国新增确诊病例数 2020.1.20-2.9 num=[5,44,62, ...

  6. 系统学习python第三天学习笔记

    day02补充 运算符补充 in value = "我是中国人" # 判断'中国'是否在value所代指的字符串中. "中国"是否是value所代指的字符串的子 ...

  7. Java之线程通信的应用:经典例题:生产者/消费者问题

    /** * 线程通信的应用:经典例题:生产者/消费者问题 * * 生产者(Productor)将产品交给店员(Clerk),而消费者(Customer)从店员处取走产品, * 店员一次只能持有固定数量 ...

  8. Python判断一个字符串是否包含子串的几种方法

    转自---http://blog.csdn.net/yl2isoft/article/details/52079960 1.使用成员操作符 in >>> s='nihao,shiji ...

  9. 架构之道(5) - APP和Web的后台架构

    当一个项目,同时需要Web.手机H5.Android,三平台同时可以测览,那就需要很简洁而有力的架构. 而我这就经历了这麽一个项目,先开发网站,然后是手机H5,最后是Android. 自信男人,无须多 ...

  10. VScode安装后的插件安装

    杭州SEO:Chinese(Simplified) Language Pack for Visual Stidio Code 中文汉化包 对于一些英文不太好的小伙伴,上来第一件事肯定是要切换成中文语言 ...