Translate

Friday, 23 January 2015

spoj PRIME1(prime generator) spoj solution

HII guys this is very good problem for beginners ................
Here simple sieve easily passed.........

HERE IS MY AC C++ SOLUTION IS:::::
IF U HAVE ANY DOUBT IN THIS U CAN ASK ME IN COMMENT....



#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define lim 31622
vector<bool> b((lim>>1)+100);
int p[5000100];
void prime()
{
 for(int i=3;i<=177;i+=2)
 {
  if(b[(i-3)>>1]==0)
  {
   for(int j=i*i;j<=lim;j+=i)
   {
    if(j&1)
     b[(j-3)>>1]=1;
   }
  }
 }
  p[0]=2;
  int count=1;
  for(int i=0;2*i<lim;i++)
   if(!b[i])
    p[count++]=2*i+3;
}
int main()
{
 int t;
 prime();
 scanf("%d",&t);
 while(t--)
 {
  LL m,n,temp,i;
  scanf("%lld %lld",&m,&n);
  if(m==1)
  m++;
  int flag;
  for(;m<=n;m++)
  {
   temp=sqrt(m);
   flag=0;
   for(i=0;p[i]<=temp;i++)
   {
    if(m%p[i]==0)
    {
     flag=1;
     break;
    }
   }
   if(flag==0)
   printf("%lld\n",m);
  }
  printf("\n");
 }
 return 0;
}

No comments:

Working With Java Collections