Primes

Let m and n be two integers, 2 ≤ m < n ≤ 10000000. Consider the following set:
P rime(m, n) = {p|p prime and m ≤ p ≤ n}.
Compute the cardinal of the set P rime(m, n).
Input
The input consists of several tests. The input of each test is represented on a single line in the input.
Any two consecutive tests are separated by an empty line. For each test, the values for m and n are
given on the same line, separated by exactly one space.
Output
For each test, the result will be written to standard output on a different line (the tests will have the
same order as in the input file). The results of any two consecutive tests will be separated by an empty
line. For each test, the result will be the cardinal of the set Prime(m, n).
Sample Input
2 20
70 110
5 150
Sample Output
8
10
33

Regionals 2012 >> Europe - Southeastern

问题链接:UVALive6050 Primes。

题意简述:参见上文。

问题分析:用Eratosthenes筛选法筛选素数,然后计算素数数量的前缀和,根据输入的数据范围做个减法即可。

程序说明:程序中有一个坑,需要注意。

参考链接:(略)

AC的C++语言程序如下:

/* UVALive6050 Primes */#include <iostream>
#include <string.h>
#include <math.h>using namespace std;const int N = 10000000;
int prefixsum[N+1];// Eratosthenes筛选法+计算前缀和
void sieveofe(int n)
{memset(prefixsum, 0, sizeof(prefixsum));prefixsum[0] = prefixsum[1] = 1;for(int i=2; i<=sqrt(n); i++) {if(!prefixsum[i]) {for(int j=i*i; j<=n; j+=i)  //筛选prefixsum[j] = 1;}}prefixsum[0] = 0;for(int i=1; i<=n; i++)if(prefixsum[i])prefixsum[i] = prefixsum[i - 1];elseprefixsum[i] = prefixsum[i - 1] + 1;
}int main()
{sieveofe(N);int n, m;bool flag = false;while(cin >> n >> m) {if(n>m)             // 坑:没说哪个数大swap(n, m);if(flag)cout << endl;cout << prefixsum[m] - prefixsum[n - 1] << endl;flag = true;}return 0;
}

UVALive6050 Primes【素数筛选+前缀和】相关推荐

  1. POJ3978 Primes【素数筛选+前缀和】

    Primes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4075   Accepted: 1579 Descriptio ...

  2. python使用集合实现筛选法求素数-python素数筛选法浅析

    原理: 素数,指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数.在加密应用中起重要的位置,比如广为人知的RSA算法中,就是基于大整数的因式分解难题,寻找两个超大的素数然后相乘作 ...

  3. 素数筛选法(埃氏筛 欧拉筛)

    质数筛选法 文章目录 质数筛选法 前言 一.埃氏筛 O(nloglogn)O(nloglogn)O(nloglogn) 二.欧拉筛O(n)O(n)O(n) 总结 前言 当需要大范围内的素数时,例如1e ...

  4. Python:实现prime sieve eratosthenes埃拉托斯特尼素数筛选法算法(附完整源码)

    Python:实现prime sieve eratosthenes埃拉托斯特尼素数筛选法算法 # flake8: noqa def prime_sieve_eratosthenes(num):prim ...

  5. 素数筛选以及优化分析

    素数筛选法的思想:对于不超过N的每个正整数,删除2P,3P,4P--,当处理完所有数之后,还没有被删除的就是素数. 按照上述思想写成的简单筛选法代码如下: memset(vis,o,sizeof(vi ...

  6. C语言素数筛选法(prime seive) 算法(附完整源码)

    素数筛选prime seive算法 C语言素数筛选prime seive算法完整源码(定义,实现,main函数测试) C语言素数筛选prime seive算法完整源码(定义,实现,main函数测试) ...

  7. 因子和(类素数筛选法)

    (忙着A题,总结会慢慢跟上~) 1409: 因子和 Time Limit: 1 Sec Memory Limit: 128 MB [Submit][Status][Web Board] Descrip ...

  8. 51nod 1536不一样的猜数游戏 思路:O(n)素数筛选法。同Codeforces 576A Vasya and Petya‘s Game。

    废话不多说,先上题目. 51nod Codeforces 两个其实是一个意思,看51nod题目就讲的很清楚了,题意不再赘述. 直接讲我的分析过程:刚开始拿到手有点蒙蔽,看起来很难,然后......然后 ...

  9. pku 2635 The Embarrassed Cryptographer 数论——素数筛选法+模拟大数除法

    http://poj.org/problem?id=2635 因为给定的k是两个素数的乘机,所以该数所包含的因子是{1,K,p,q}假设k = p*q p,q为素数,所以只要从小到大枚举小于L的素数, ...

最新文章

  1. [转载]二叉树(BST,AVT,RBT)
  2. vim自动跳转到引用的函数
  3. 机械装备计算机控制技术考试题,武汉理工大学机电工程学院研究生课程考试试题(肖峻)...
  4. docker xware下载慢_win7环境下Docker快速构建及阿里云容器加速配置
  5. matlab平滑曲线_梯度下降法实现路径平滑
  6. laravel-mix打包 js css
  7. 线性目标规划(线性目标规划、图解法、单纯形法)
  8. sscanf提取字符串数字
  9. jmeter 运行接口报javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection的解决方法
  10. 根据设备不同,加载不同尺寸图片
  11. STM32开发利器:STM32CubeMX
  12. 计算机认知矫正发展史,计算机认知矫正疗法对儿童认知功能的影响.pdf
  13. 中国义乌进口商品博览会秋季展落幕 成交额达2.7亿元
  14. Scrapy爬取知乎Python专题精华,连答主头像都给爬下来,不放过一切
  15. 【简单算法】47.帕斯卡三角形
  16. Python Magic——文件操作
  17. PM2入门及其常用命令
  18. AC1082带标准程序的版本说明_2.硬件以及参数说明
  19. OCI 与容器镜像构建
  20. 数据治理-数据生命周期管理-大数据采集

热门文章

  1. 600个开源iOS应用库
  2. 手撕Vue-Router
  3. 怎么查看linux硬盘多路径,linux下磁盘多路径
  4. python repusts模块_Python tslearn包_程序模块 - PyPI - Python中文网
  5. Scala的初步学习(一)
  6. 安装JDK失败,再次安装时出现已经安装过了的,解决办法
  7. Spark中DataFrame 基本操作函数
  8. SequenceFile文件的读取
  9. 使用shiro框架的项目增加忘记密码功能遇到的一些问题
  10. oracle 索引-1无效,oracle 索引失效原因及解決方法