I solved the prime1 problem on spoj. I am getting the write answer for testcase-1 but nothing to testcase-2. I don't know what mistake i am making in here, i always get this type of problem on the same problem, till now i was not able to solve it.
PRIME1 - Prime Generator
number-theory #sieve-of-eratosthenes
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
Input The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
Example Input: 2 1 10 3 5
Output: 2 3 5 7
3 5
Here is my code for the prime1 problem from spoj.
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long int
using namespace std;
void prime1(ll n, ll m){
bool prime[m+1];
memset(prime,true,sizeof(prime));
for(int p = 2; p * p <= m; p++ ){
if(prime[p] == true){
for(int i = p * 2; i <= m; i+=p){
prime[i] = false;
}
}
}
//print prime list
for(int i = 2; i <= m; i++)
{
if(prime[i]){
cout << i << "\n";
}
}
}
int main(){
int t;
ll n,m;
cin >> t;
for(int i = 0 ; i < t ;i++){
cin >> n >> m;
prime1(n,m);
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire