1695:找素数
来源:互联网 发布:人工智能简介 编辑:程序博客网 时间:2024/06/11 22:51
1695:找素数
Description
算法提高 找素数时间限制:1.0s 内存限制:256.0MB
问题描述
给定区间[L, R] , 请计算区间中素数的个数。
输入格式
两个数L和R。
输出格式
一行,区间中素数的个数。
样例输入
2 11
样例输出
5
数据规模和约定
2 < = L < = R < = 2147483647 R-L < = 1000000
#include<cstdio> #include<iostream> #include<algorithm> #include<cmath> using namespace std; const int MAX_L = 9999999; const int MAX_SQRT_B = 9999999; typedef long long ll; bool is_prime[MAX_L], is_prime_small[MAX_SQRT_B]; int sum = 0; void segment_sieve(ll a, ll b) { for (int i = 0; (ll)i * i < b; i++) is_prime_small[i] = true; for (int i = 0; i < b - a; i++) is_prime[i] = true; for (int i = 2; (ll)i * i < b; i++) { if (is_prime_small[i]) { for (int j = 2*i; (ll)j*j < b; j += i) is_prime_small[j] = false; for (ll j = max(2LL, (a+i-1)/i)*i; j < b; j += i) { is_prime[j-a] = false; } } } return; } int main() { ll a, b; cin >> a >> b; segment_sieve(a, b+1); for (ll i = a; i <= b; i++) { if (is_prime[i-a]) sum++; } printf("%d\n", sum); return 0; }
阅读全文
0 0
- 1695:找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- 找素数
- Eratosthenes 找素数
- 找素数算法总结
- 数据结构之线性表
- Android新浪微博分享
- 1173:寻找2的幂
- java-rabbitmq-windows部署,即常用命令
- GoodSync 正版购买 特惠 特价 技术支持 中文教程 bbs.goodsync.cc
- 1695:找素数
- Redis Shell
- 嵌入式系统使用keepalived实现双机热备功能
- 1214:单目标01背包问题
- 【深度学习Deep Learning】资料大全
- Java-导论2
- [Android] 彻底了解Binder机制原理和底层实现
- 笔记本建立wifi热点
- javascript数组 去掉重复的元素 两个方法效率对比分析