题意
求出lll到rrr中质数的个数和由两个质数乘起来得到的数的个数总和,有ttt组数据。
思路
先筛质数,再把满足条件的数标记起来,用前缀和求区间的总和。
代码
#include<cstdio>
#include<cstring>
using namespace std;
const int MAXN = 10000000;
int q, l, r;
int s[MAXN + 1];
bool isLike[MAXN + 1], isPrime[MAXN + 1];
void fastIn(int &a) {
a = 0;
char c = getchar();
while (c > '9' || c < '0') c = getchar();
while (c >= '0' && c <= '9') a = a * 10 + c - 48, c = getchar();
}
void fastOut(int a) {
if (a > 9) fastOut(a / 10);
putchar(a % 10 + 48);
}
void init() {
memset(isPrime, 1, sizeof(isPrime));
isPrime[0] = isPrime[1] = 0;
for (int i = 2; i <= MAXN; i++) {
if (!isPrime[i]) continue;
isLike[i] = 1;
for (int j = 2; j * i <= MAXN; j++) {
if (j <= i && isPrime[j])
isLike[i * j]=1; //标记满足条件
isPrime[i * j] = 0;//筛
}
}
for (int i = 1; i <= MAXN; i++)
s[i] = s[i - 1] + isLike[i];//前缀
}
int main() {
init();
fastIn(q);
while (q--) {
fastIn(l);fastIn(r);
fastOut(s[r] - s[l - 1]);
putchar(10);
}
}