5458. 【NOIP2017提高A组冲刺11.7】质数
(File IO): input:prime.in output:prime.out
Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits
Goto ProblemSet做法:当我们使用线性筛的时候,会发现和本题奇妙的契合,emmm,然后跑前缀和直接输出答案。
#include <cstdio> #include <cstring> #include <iostream> #define N 10000020 using namespace std; int q, zs[N / 2], qzh[N], n; bool b[N], v[N]; void Pre_work() { memset(qzh, 0, sizeof(qzh)); for (int i = 2; i <= N - 10; i++) { if (!b[i]) { zs[++zs[0]] = i; v[i] = 1; for (int j = 1; j <= zs[0]; j++) if (i * zs[j] > N - 10) break; else v[i * zs[j]] = 1, b[i * zs[j]] = 1; } else { for (int j = 1; j <= zs[0]; j++) if (i * zs[j] > N - 10) break; else b[i * zs[j]] = 1; } } for (int i = 1; i <= N - 10; i++) qzh[i] += qzh[i - 1] + v[i]; } int main() { freopen("prime.in", "r", stdin); freopen("prime.out", "w", stdout); Pre_work(); scanf("%d", &n); int x, y; for (; n; n--) { scanf("%d%d", &x, &y); printf("%d ", qzh[y] - qzh[x - 1]); } }