Created
July 6, 2013 19:05
-
-
Save niviusha/5940895 to your computer and use it in GitHub Desktop.
This is the SPOJ Problem PRIME1.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
#include<cstdio> | |
#include<cmath> | |
#include<cstring> | |
#include<vector> | |
typedef long long ll; | |
void segseive( ll l, ll r){ | |
ll limit = sqrt(r), diff = limit+1, arr[diff],i,j, seivearr[1000], start, num, set; | |
memset(arr, 0 , sizeof(arr)); | |
vector<ll> primes; | |
vector<ll>::iterator it; | |
arr[0] = arr[1] = 1; | |
for( i = 0; i <= limit; i++){ | |
if( !arr[i] ){ | |
primes.push_back(i); | |
for( j = i+i; j<=limit; j+= i){ | |
arr[j] = 1; | |
} | |
} | |
} | |
for( i=l; i<=r; i+= 1000){ | |
start = i; | |
memset(seivearr, 0 , sizeof(seivearr)); | |
for(it = primes.begin(); it != primes.end(); it++){ | |
num = *it; | |
set = num - (start %num); | |
if( start%num ==0 && start>num) | |
seivearr[0] = 1; | |
for( j = set ; j<1000 && start + j<=r; j+= num){ | |
if( start+j <= num) | |
continue; | |
seivearr[j] = 1; | |
} | |
} | |
for( j = 0; j< 1000 && j+start <= r; j++){ | |
num = j+start; | |
if( !seivearr[j] && num != 0 && num != 1) | |
printf("%lld\n", start+j); | |
} | |
} | |
cout<<endl; | |
} | |
int main(){ | |
ll l, r; | |
int t; | |
for( scanf("%d", &t); t; t--){ | |
scanf("%lld%lld", &l, &r); | |
segseive(l,r); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment