code
#include<iostream>
#include<algorithm>
#include<complex>
using namespace std;
const int N= 2000000;
int p[N+1]={0};
unsigned long long S[N+1];
int main(){
int i,j,t,n;
unsigned long long s;
p[0]=p[1]=1;
t=floor(sqrt(N)+.5);//Rounds x downward, returning the largest integral value that is not greater than x.
for(i=2;i<=t;++i){
if(!p[i]){
for(j=i*i;j<=N;j+=i){
p[j]=1;//mark composite number
}
}
}
s=0;
for(i=0;i<=N;++i){
if(!p[i]){
s+=i;
}
S[i]=s;
}
while(~scanf("%d",&n)){
printf("%llu\n",S[n]);
}
return 0;
}
Q
蓝桥杯算法提高VIP-素数求和
时间限制: 1Sec 内存限制: 128MB 提交: 3789 解决: 995
题目描述
输入一个自然数n,求小于等于n的素数之和
输入
数据规模和约定
测试样例保证 2 < = n < = 2,000,000
输出
无
样例输入
2
样例输出
2