题目传送门

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. php封装一个接口类

    <?phpClass Response{//返回数据 public static function show($code,$message='',$data='',$type = 'json', ...

  2. Linux 常用命令全拼

    pwd: print work directory 打印当前目录 显示出当前工作目录的绝对路径 ps: process status(进程状态,类似于windows的任务管理器) 常用参数:-auxf ...

  3. 2.react 基础 - create-react-app 目录结构 及 组件应用

    1. react-app 脚手架的 目录结构 node_modules -d 存放 第三方下载的 依赖的包 public -d    资源目录 favicon.ico - 左上角的图标 index.h ...

  4. gradle问题

    1, my gradle version is 4.6 . in project.gradle : change dependencies { classpath 'com.android.tools ...

  5. ccf201403-3 记录一个神tmwa了的代码 莫非我没看懂题。。。

    #include <string.h> #include<cstdio> #include<stdio.h> #include <iostream> # ...

  6. node 第三方库总结

    app.post("/todo/add", (request, response) => { request.body //如何拿到前端ajax传来的JSON数据 }) 需要 ...

  7. Linux-sys文件系统

    1.sys文件系统本质上和proc文件系统是一样的,都是虚拟文件系统.都在根目录下有个目录(一个是/proc目录,另一个是/sys目录),因此都不是硬盘中的文件,都是内核中的数据结构的可视化接口. 2 ...

  8. JS - ES5与ES6面向对象编程

    1.面向对象 1.1 两大编程思想 1.2 面向过程编程 POP(Process-oriented programming) 1.3 面向对象编程 OOP (Object Oriented Progr ...

  9. dubbo配置文件加载顺序

    JVM 启动 -D 参数优先,这样可以使用户在部署和启动时进行参数重写,比如在启动时需改变协议的端口. XML 次之,如果在 XML 中有配置,则 dubbo.properties 中的相应配置项无效 ...

  10. 通过Dockerfile 文件为linux images 添加新用户

    要求: (1)增加一个新用户,名为mynewuser (2)让这个用户有root权限 (3)设置其密码为mynewpassword (4)Container启动后以mynewuser登录,并且直接到m ...