题目传送门

sol1:普通判到sqrt(n)的素数判定,不多说了。

  • 素数判定

    #include "bits/stdc++.h"
    using namespace std;
    bool is_prime(int n) {
    for (int i = ; 1LL * i * i <= n; i++)
    {
    if (n % i == ) return false;
    }
    return true;
    }
    int main() {
    int n, m;
    while (~scanf("%d", &n)) {
    int cnt = ;
    for (int i = ; i <= n; i++) {
    scanf("%d", &m);
    if (is_prime(m)) cnt++;
    }
    printf("%d\n", cnt);
    }
    return ;
    }

    复杂度sqrt(m),判断n次就是n * sqrt(m);

sol2:新get到的技巧Miller-Rabin素数测试,结合了费马小定理和二次探测定理,可以更高效的判断素数,存在误判可能,不过误判可能非常小,可以忽略不计;

  • Miller-Rabin素数测试

    #include "bits/stdc++.h"
    using namespace std;
    int quick_pow(int n, int k, int p) {
    int ans = ;
    while (k) {
    if (k & ) ans = 1LL * ans * n % p;
    n = 1LL * n * n % p;
    k >>= ;
    }
    return ans;
    }
    bool is_prime(int n) {
    // if (n < 2) return false;
    int s = , t = n - ;
    while (!(t & )) s++, t >>= ;
    for (int i = ; i <= ; i++) {
    int a = rand() % (n - ) + ;
    int k = quick_pow(a, t, n);
    for (int j = ; j <= s; j++) {
    int x = 1LL * k * k % n;
    if (x == && k != && k != n - ) return false;
    k = x;
    }
    if (k != ) return false;
    }
    return true;
    }
    int main() {
    int n, m;
    srand(time(NULL));
    while (~scanf("%d", &n)) {
    int cnt = ;
    for (int i = ; i <= n; i++) {
    scanf("%d", &m);
    if (is_prime(m)) cnt++;
    }
    printf("%d\n", cnt);
    }
    return ;
    }

    复杂度logm,判断n次就是n * log(m);

附加一个用于 LL 范围素数测试的模板:s

  • Miller-Rabin素数测试 LL 范围模板

    typedef long long LL;
    LL quick_mul(LL n, LL k, LL p) {
    LL ans = ;
    while (k) {
    if (k & ) ans = (ans + n) % p;
    n = (n + n) % p;
    k >>= ;
    }
    return ans;
    }
    LL quick_pow(LL n, LL k, LL p) {
    LL ans = ;
    while (k) {
    if (k & ) ans = quick_mul(ans, n, p);
    n = quick_mul(n, n, p);
    k >>= ;
    }
    return ans;
    }
    bool is_prime(LL n) {
    if (n < ) return false;
    LL s = , t = n - ;
    while (!(t & )) s++, t >>= ;
    for (int i = ; i <= ; i++) {
    LL a = rand() % (n - ) + ;
    LL k = quick_pow(a, t, n);
    for (int j = ; j <= s; j++) {
    LL x = quick_mul(k, k, n);
    if (x == && k != && k != n - ) return false;
    k = x;
    }
    if (k != ) return false;
    }
    return true;
    }

    大致差不多,为了防止爆 LL 加了一个快速积用于乘, 所以复杂度变成了log(m) * log(m)

HDU-2138-How many prime numbers(Miller-Rabin新解法)的更多相关文章

  1. HDOJ(HDU) 2138 How many prime numbers(素数-快速筛选没用上、)

    Problem Description Give you a lot of positive integers, just to find out how many prime numbers the ...

  2. HDU 2138 How many prime numbers(Miller_Rabin法判断素数 【*模板】 用到了快速幂算法 )

    How many prime numbers Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  3. HDU 2138 How many prime numbers

    米勒罗宾素数测试: /* if n < 1,373,653, it is enough to test a = 2 and 3. if n < 9,080,191, it is enoug ...

  4. HDU 2138 How many prime numbers (判素数,米勒拉宾算法)

    题意:给定一个数,判断是不是素数. 析:由于数太多,并且太大了,所以以前的方法都不适合,要用米勒拉宾算法. 代码如下: #include <iostream> #include <c ...

  5. 【HDU】2138 How many prime numbers

    http://acm.hdu.edu.cn/showproblem.php?pid=2138 题意:给n个数判断有几个素数.(每个数<=2^32) #include <cstdio> ...

  6. hdu 5108 Alexandra and Prime Numbers(水题 / 数论)

    题意: 给一个正整数N,找最小的M,使得N可以整除M,且N/M是质数. 数据范围: There are multiple test cases (no more than 1,000). Each c ...

  7. hdu 5108 Alexandra and Prime Numbers

    数论题,本质是求出n的最大质因子 #include<time.h> #include <cstdio> #include <iostream> #include&l ...

  8. HDU 3864 D_num Miller Rabin 质数推断+Pollard Rho大整数分解

    链接:http://acm.hdu.edu.cn/showproblem.php? pid=3864 题意:给出一个数N(1<=N<10^18).假设N仅仅有四个约数.就输出除1外的三个约 ...

  9. POJ1811- Prime Test(Miller–Rabin+Pollard's rho)

    题目大意 给你一个非常大的整数,判断它是不是素数,如果不是则输出它的最小的因子 题解 看了一整天<初等数论及其应用>相关部分,终于把Miller–Rabin和Pollard's rho这两 ...

  10. poj 1811 Pallor Rho +Miller Rabin

    /* 题目:给出一个数 如果是prime 输出prime 否则输出他的最小质因子 Miller Rabin +Poller Rho 大素数判定+大数找质因子 后面这个算法嘛 基于Birthday Pa ...

随机推荐

  1. SQL基础教程(第2版)第4章 数据更新:4-1 数据的插入(INSERT)

    第4章 数据更新:4-1 数据的插入(INSERT) ● 将列名和值用逗号隔开,分别括在()内,这种形式称为清单.● 对表中所有列进行INSERT操作时可以省略表名后的列清单.● 插入NULL时需要在 ...

  2. 关于PHP索引数组unset某key后json_encode相关问题踩坑记录

    <?php $a = [1,2,3]; var_dump(json_encode($a)); #string(7) "[1,2,3]" unset($a[0]); var_d ...

  3. rsync搭建

    服务器: 查看是否安装:rpm -qa rsync 未安装则:yum install -y rsync 添加rsync用户 useradd -s /sbin/nologin -M rsync 编辑/e ...

  4. cuda cudaTextureFilterMode

    cudaFilterModePoint:点模式,返回最接近的一个点,即最近邻插值.插值公式 tex(x) = T(i),i=floor(x),注意是对坐标向下取整,所以一般对输入坐标值+0.5,避免无 ...

  5. java 练习题带答案

    第一题 int x = 1,y=1; if(x++==2 & ++y==2) { x =7; } System.out.println("x="+x+",y=&q ...

  6. day65-CSS选择器和样式优先级

    1. CSS CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素. 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). 2.CSS语法 每个 ...

  7. JavaScript详解(二)

    js的流程控制 if语句: if (条件表达式A){ xx; }else if (条件表达式B){ xx; } else{ xx; } switch语句: switch (表达式){ case 值1: ...

  8. Matlab高级教程_第二篇:Matlab相见恨晚的模块_02_并行运算-1

    1 更高级的算法牵扯到更多重的循环和复杂的计算,尤其是现在人工智能的算法尤其如此.有些历史知识的人能够了解到,人工智能的很多基本算法其实近百年之前就有了,但是当时的计算机技术达不到去实现这些算法的要求 ...

  9. goweb-处理静态资源

    处理静态文件 对于 HTML 页面中的 css 以及 js 等静态文件,需要使用使用 net/http 包下的以下 方法来处理 1) StripPrefix 函数 2) FileServer 函数 3 ...

  10. HDU重现赛之2019CCPC-江西省赛

    本人蒟蒻,5个小时过了5道,看到好几个大佬AK,%%%%%%% http://acm.hdu.edu.cn/contests/contest_show.php?cid=868 先放大佬的题解(不是我写 ...