Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Sunday, January 11, 2015

FINDPRM-Finding Primes

Finding Primes

given below code is for findprm or finding primes spoj .


Here I have applied basic sieve to find primes . Here you have to tell the numbers of prime between N/2 and N . because all primes before N/2 are not common with second sieve(stated in question).



#include <bits/stdc++.h>
using namespace std;
bool primes[10000009];
int cum[10000009];
void pre(){
    for(int i = 3 ; i<= 3163 ; i+=2){
        if(!primes[i]){
            for(int j = i*i ; j<= 10000000 ; j+=2*i)
                primes[j] = 1;
        }
    }
    cum[2] = 1;
    for(int i = 3 ; i<=10000000 ; i++){
        if(primes[i] == 0 && i%2 != 0)
            cum[i] += cum[i-1] + 1;
        else
            cum[i] = cum[i-1];
    }
}
int main(){
    int t;
    pre();
    scanf("%d",&t);
    while(t--){
        int n;
        scanf("%d",&n);
        printf("%d\n" , cum[n] - cum[(n>>1)]);
    }
    return 0;
}

No comments:

Post a Comment

Your comment is valuable to us