【欧拉计划】10. Summation of primes

(本题取 $n=2000000$)

【思路】$\mathcal O(n)$ 欧拉筛:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>
#include<stdbool.h>
const int maxn=2000000;
int prime[maxn];
bool vis[maxn];
long long ans;
void euler()
{
for(int i=2;i<=maxn;++i)
{
if(!vis[i])
{
prime[++prime[0]]=i;
ans+=i;
}
for(int j=1;i*prime[j]<=maxn&&j<=prime[0];++j)
{
vis[i*prime[j]]=true;
if(i%prime[j]==0)break;
}
}
}
int main()
{
euler();
printf("%lld\n",ans);
return 0;
}

【欧拉计划】10. Summation of primes

https://hensier.github.io/projecteuler/10/

作者

hensier

发布于

2022-05-01

更新于

2023-01-02

许可协议

评论