Title Address: http://acm.fafu.edu.cn/problem.php?id=1011
Description:
The problem is very simple,your job was just to calculate the sum of primes by 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
The title means to enter an n, calculated from the first 1 primes to the nth prime number and. Pay attention to the topic,N maximum may reach million. In other words, the test data may allow you to calculate the number of the number from the first 1 primes to The first number of primes. The difficulty of this question is how to quickly filter out primes. If we use the enumeration method (or the normal method), it must be timed out. Here, I would like to introduce a large prime sieve (specially screened prime numbers).
We know 2,5,7 ... these are prime numbers. And the prime number of multiples must not be prime, then we just have to judge the number of prime number of the speed of the deletion can be. An example:2,3,4,5,6,7,8,9,10,11,12,13,15,16,17,18,19,20.
(1) Judging whether 2 is a prime number; deleting the multiples of 2 and Remaining:
2,3,5,7,9,11,13,15,17,19
(2) Judging whether 3 is a prime number; deleting the multiples of 3 and Remaining:
2,3,5,7,11,13,17,19
(3) to determine whether 5 is prime, yes; Delete the multiples of 5 . And so on .
So how is the code implemented? Let's say we ask for the first 1 to the number of primes you can define a isprime Array to mark whether it is a prime number. Before determining the size of the array, we need to determine How big the first number of primes might be. Here we assume the first .
for 2 ; ++i)// here I<=sqrt (400) that is 400 of the radical if(isprime[i ])for2 ; j+=i) // I+i is a multiple of I false; // multiples of prime number 0
View Code
Mark,isprime[i] equals 1 , is the prime number, and the prime number is its subscript. You can then get the prime number.
for 2, k=1; + +i )if(Map[i]) prime[k++] = i ; // starting from subscript 1, the first prime number is stored
View Code
if (Map[i]) prime[k++] = i;//The First prime number starting from subscript 1
The rest of the work is much easier! The overall code is as follows:
#include <cstdio>#include<cstring>#defineN 1000000#defineMAXN 16000000BOOLMap[maxn];__int64 prime[n+5]={0};__int64 sum[n+5]={0};intMainvoid) {memset (map,true,sizeof(map)); inti,j,n,k=1; for(i =2; I <=4000; ++i)if(Map[i]) for(j =2*i; J < Maxn; J+=i)//Place a multiplier of prime number 0MAP[J] =false; for(i =2, j =1; I <= maxn && J <= N; ++i)if(Map[i]) {prime[k+ +] = i;//starting from subscript 1, the first prime number is storedSUM[J] = sum[j-1]+PRIME[J]; ++j;//Sum[k] for the first and the 1~k primes } while(SCANF ("%d", &n)! =EOF) printf ("%i64d\n", Sum[n]); return 0;}View Code
In this code, I use the map array to represent the state (that is, the function of the IsPrime array) and the prime array to hold the primes. Sum array to save the result. The value of such as sum[10] is the number of the 1th prime to the 10th Prime.
Large Prime sieve