素数之和
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:
1
3
5
Sample Output:
2
10
28
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;
}