典型的区间筛例题,当时不知道这玩意,痛失好题
链接:https://ac.nowcoder.com/acm/contest/18428/E
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N = 5e6 + 10;
typedef long long LL;
int vis[N], pri[N], cnt;
LL p[N];
bool f[N];
int pr[N];
void init(LL s, LL e)
{
for (LL i = 1; i <= cnt&&pri[i] * pri[i] <= e; i++)
{
for (LL j = max(2LL, (s + pri[i] - 1) / pri[i])*pri[i]; j <= e; j += pri[i]) {
if (j >= s){
f[j - s] = true;
if (!pr[j - s]) pr[j - s] = pri[i];
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
for (int i = 2; i <= N - 5; i++) {
if (!vis[i]) {
pri[++cnt] = i;
}
for (int j = 1; j <= cnt && i*pri[j] <= N - 5; j++) {
vis[i*pri[j]] = 1;
if (i%pri[j] == 0) break;
}
}
LL s, e;
int k;
scanf("%lld%lld%d", &s, &e, &k);
init(s, e);
int t=0;
for (LL i = 0; i <= e-s; i++)
{
if (!f[i])
p[t++] = i+s;
else
p[t++] = pr[i];
}
sort(p, p+t);
long long res = 0;
for (int i = 0; i < k; i++){
res += p[i];
}
printf("%lld", res);
return 0;
}