I have just done some improvments, but it doesn't enough.
#include<stdio.h>
#include<vector>
typedef unsigned long ul;
int main()
{
int t;
scanf("%d", &t);
for (int main_i = 0; main_i < t; main_i++)
{
ul m, n;
scanf("%lu%lu", &m, &n);
std::vector<bool>sieve(n+1, true);
ul m_half=(m>3)?m/2:m;
for (ul i = 2; i*i <= n; ++i)
if (sieve[i])
for (ul j = m_half; i*j <= n; ++j)
sieve[i*j] = false;
if(m==2){
printf("%d\n",2);
for (ul i = 3; i <= n; i+=2)
if (sieve[i]){
printf("%lu\n", i);
}
}
else{
if(!(m%2))
++m;
for (ul i = m; i <= n; i+=2)
if (sieve[i]){
printf("%lu\n", i);
}
}
putchar('\n');
}
return 0;
}
After that I rewrited the use of the vector, but I didn't see any improvments, especially with low number input.
#include<stdio.h>
#include<vector>
typedef unsigned long ul;
int main()
{
std::vector<bool>sieve(1000000000,true);
ul max_m=2;
int t;
scanf("%d", &t);
for (int main_i = 0; main_i < t; main_i++)
{
ul m, n;
scanf("%lu%lu", &m, &n);
for (ul i = 2; i*i <= n; i++)
if (sieve[i]){
for (ul j = max_m; j <= n/2; j++)
sieve[i*j] = false;
}
if(max_m<m)
max_m=m;
for(ul i=m;i<=n;++i)
if(sieve[i])
printf("%lu\n",i);
putchar('\n');
}
return 0;
}
Would you like to give me some more advices? Is there any chance to do this with Eratosthenes? Or there are other simplier and faster methods?