UVALive6050 Primes【素数筛选+前缀和】
2017-06-15 08:56
337 查看
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++语言程序如下:
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]; else prefixsum[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; }
相关文章推荐
- POJ3978 Primes【素数筛选+前缀和】
- UVALive3399 UVA1210 POJ2739 Sum of Consecutive Prime Numbers【素数筛选+尺取法】
- UVA583 UVALive5406 Prime Factors【素数因子+筛选法】
- UVA10200-Prime Time/HDU2161-Primes,例题讲解,费马小定理和欧拉函数判素数。
- UVALive - 7635(前缀和)
- UVa 10539 - Almost Prime Numbers(筛选求素数)
- Uva 10168 Summation of Four Primes 素数
- UVA10168 Summation of Four Primes【筛选法】
- codeforces-385C Bear and Prime Numbers(素数筛选法+维护前缀和)
- UVALive 3026 Period ( kmp 求前缀最小循环节)
- UVALIve 5987 素数
- UVa 11426 GCD - Extreme (II) / 素数筛选 + 欧拉函数
- UVA - 10394 - Twin Primes(素数对)
- UVALive 2678 利用序列的前缀来减少时间复杂度
- UVa1213 - Sum of Different Primes(素数表+DP)
- Count Primes 筛选法求素数
- hdoj 4715 Difference Between Primes 素数筛选+二分查找
- UVA - 1404 Prime k-tuple (素数筛选)
- UVa 1644 - Prime Gap(筛选求素数)
- 习题10-5 UVA - 1213 Sum of Different Primes 不同的素数之和(DP + 素数打表)