You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
//欧拉筛
const int N = 1e5 + 10;
int primes[N], cnt; // primes[]存储所有素数
bool st[N]; // st[x]存储x是否被筛掉
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[cnt++] = i;
for (int j = 0; primes[j] * i <= n; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
}
//区间范围,因为我们无法完全映射所有的区间只能采用类似于偏移的办法对某段区间整体偏移L进行描述。
//否则空间上的限制就先达到了,无法用计算机模拟了。
const int M = 10000010;
int a[M];//记录偏移后的数据是不是合数1合数0质数。a[i]表示L+i是不是合数, 有一个偏移量L
int main() {
//筛出50000之内的所有质数
get_primes(50000);//R开根号的极限也小于50000
//问为啥要开LL开INT不行吗
//答不行因为下面的运算中可能存在加法如果是极限的整数再加一下就会爆INT。
LL L, R;
cin >> L >> R;
//特判防止第11个测试点WA掉。
if (L == 1) L = 2;
//遍历已知的质数列表
for (int i = 0; i < cnt; i++) {
//start找到开始筛的数字
//【大于L,并且是p的倍数,最小整数是多少?】
LL start = max(2ll, (L - 1) / primes[i] + 1) * primes[i];
//成倍的质数筛出掉
for (LL j = start; j <= R; j += primes[i]) a[j - L] = 1; //标识为质数
}
//结果
int ans = 0;
for (LL i = L; i <= R; i++) if (!a[i - L])ans++;
printf("%d", ans);
return 0;
}