FAFU OJ 素数之和
原创
©著作权归作者所有:来自51CTO博客作者兔云程序的原创作品,请联系作者获取转载授权,否则将追究法律责任
素数之和
Time Limit: |
3000MS |
Memory Limit: |
65536KB |
Total Submissions: |
2067 |
Accepted: |
475 |
Share
Description:
The problem is very simple,your job is just to calculate the sum of primes from the first prime to the Nth prime.
Input:
The input consists multiple cases. Each line contains a N(1<=N<=1000000).
Output:
For each N,output the result of this problem that describe before.
Sample Input:
Sample Output:
Source:
#include<iostream>
typedef __int64 LL;
using namespace std;
const int MAXN = 1000000 + 5;
const int MAXSIZE = MAXN << 4;
int n, cnt;
LL ans[MAXN];
bool vis[MAXSIZE];
void prepare()
{
int x;
cnt = 1, ans[cnt] = 2;
for(int i = 3; i < 4000; i += 2)
if(vis[i] == false)
{
x = i << 1;
for(int j = i * i; j < MAXSIZE; j += x)
vis[j] = true;
}
for(int i = 3; i < 15485865; i += 2)
if(vis[i] == false)
ans[++ cnt] = ans[cnt - 1] + i;
}
int main()
{
prepare();
cin.sync_with_stdio(false);
while(cin >> n)
cout << ans[n] << endl;
return 0;
}