SPOJ Prime Generator
// Accepted
// Time: 0.01
// Process: Segmented Seieve
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
#define DIFF 100010
#define mset(c,v) memset(c,v,sizeof(c))
#define sf scanf
#define pf printf
LL prm[DIFF], plen=0;
void sieve(int n)
{
LL i,j;
bool mark[DIFF];
//mset(mark, true);
for(i=2; i<DIFF; i++) mark[i] = true;
LL qrt = floor(sqrt(double(n)));
LL k = floor(sqrt (double(qrt)));
for(i=2; i<=k; i++)
{
if(mark[i])
{
for(j=i*i; j<=qrt; j+=i)
{
mark[j] = false;
}
}
}
for(i=2; i<=qrt; i++)
{
if(mark[i])
{
prm[plen++] = i;
}
}
//for(i=0; i<plen; i++) cout << prm[i] << " ";
pf("\n");
}
int main()
{
bool primenow[DIFF];
int t;
sf("%d", &t);
while(t--)
{
plen=0;
LL x , y , p, s, i, j;
sf("%lld %lld", &x, &y);
sieve(y);
// mset(primenow, true);
for(i=0; i<DIFF; i++) primenow[i] = true;
for(i=0; i<plen; i++)
{
p = prm[i];
s = x / p;
s*=p;
for(j=s; j<=y; j+=p)
{
if(j < x) continue;
primenow[j-x] = false;
}
}
for(i=0; i<plen; i++)
{
if(prm[i] >= x and prm[i]<=y)
{
pf("%lld\n", prm[i]);
}
}
for(i=0; i<y-x+1; i++)
{
if(primenow[i]==true and (i+x) != 1)
{
pf("%lld\n", i+x);
}
}
}
return 0;
}
// Time: 0.01
// Process: Segmented Seieve
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
#define DIFF 100010
#define mset(c,v) memset(c,v,sizeof(c))
#define sf scanf
#define pf printf
LL prm[DIFF], plen=0;
void sieve(int n)
{
LL i,j;
bool mark[DIFF];
//mset(mark, true);
for(i=2; i<DIFF; i++) mark[i] = true;
LL qrt = floor(sqrt(double(n)));
LL k = floor(sqrt (double(qrt)));
for(i=2; i<=k; i++)
{
if(mark[i])
{
for(j=i*i; j<=qrt; j+=i)
{
mark[j] = false;
}
}
}
for(i=2; i<=qrt; i++)
{
if(mark[i])
{
prm[plen++] = i;
}
}
//for(i=0; i<plen; i++) cout << prm[i] << " ";
pf("\n");
}
int main()
{
bool primenow[DIFF];
int t;
sf("%d", &t);
while(t--)
{
plen=0;
LL x , y , p, s, i, j;
sf("%lld %lld", &x, &y);
sieve(y);
// mset(primenow, true);
for(i=0; i<DIFF; i++) primenow[i] = true;
for(i=0; i<plen; i++)
{
p = prm[i];
s = x / p;
s*=p;
for(j=s; j<=y; j+=p)
{
if(j < x) continue;
primenow[j-x] = false;
}
}
for(i=0; i<plen; i++)
{
if(prm[i] >= x and prm[i]<=y)
{
pf("%lld\n", prm[i]);
}
}
for(i=0; i<y-x+1; i++)
{
if(primenow[i]==true and (i+x) != 1)
{
pf("%lld\n", i+x);
}
}
}
return 0;
}
Comments
Post a Comment