题目传送门

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. 【转帖】使用了 Service Mesh 后我还需要 API 网关吗?

    使用了 Service Mesh 后我还需要 API 网关吗? https://www.kubernetes.org.cn/6762.html api gateway和istio 是不一样的 追求不一 ...

  2. 奔跑的绵羊js

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 常用的tensorflow函数

    在mask_rcnn常用的函数 1  tf.cast():    https://blog.csdn.net/dss875914213/article/details/86558407 2 tf.ga ...

  4. 信号分析——从傅里叶变化到FFT

    我们眼中的世界就像皮影戏的大幕布,幕布的后面有无数的齿轮,大齿轮带动小齿轮,小齿轮再带动更小的. 在最外面的小齿轮上有一个小人——那就是我们自己. 我们只看到这个小人毫无规律的在幕布前表演,却无法预测 ...

  5. java依赖包问题排查

    使用算法 breeze.optimize.LBFGSB,出现如下问题: 看到github上一个issue的解决方法是使用最新版本的 org.scalanlp:breeze_2.11:1.0-RC2, ...

  6. PAT Basic 1034 有理数四则运算(20) [数学问题-分数的四则运算]

    题目 本题要求编写程序,计算2个有理数的和.差.积.商. 输⼊格式: 输⼊在⼀⾏中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分⼦和分⺟全是整型范围内的整数, ...

  7. myeclipse跟tomcat的同步

    一般来说,我们在myeclipse里把文件内容改了并保存之后,直接刷新网页就可以非常直观的看到内容的改变. 这是因为myeclipse检测到文件内容的变动,及时地把新的文件部署到了tomcat上. m ...

  8. C++随机迷宫生成[转载]

    原文:http://tieba.baidu.com/p/2596809144 #include<iostream.h> #include"time.h" #includ ...

  9. 关于vue内只要html元素的代码

    使用v-model v-text v-html vue会解析出入的html代码报错 则上传sku的description时需要html页面的代码 所以在description 所在的表单元素内加入 v ...

  10. python学习Day08--文件操作

    [主要内容] 文件操作: 1. r 2. w 3. a 4. r+ 读写模式. 需要移动光标进行反复读写 5. w+ 6. a+ 7. b bytes 读写操作的是字节. 用在非文本上 8. seek ...