Cod sursa(job #413058)

Utilizator SpiderManSimoiu Robert SpiderMan Data 7 martie 2010 15:37:45
Problema Divizori Primi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <fstream>
using namespace std;

#define MAX 1000000
#define hg 8192

int st,dr,mij,x,i,j,t,k,n,mat[8][380000],l[8],poz;
short v[MAX+1];
char ch[hg];

FILE *f=fopen("divprim.in","r");
FILE *g=fopen("divprim.out","w");

inline void cit (int &x)
{
    x = 0;
    if (ch[0]=='\0') fread (ch, 1, hg, f);
    else while (ch[poz] < '0' || ch[poz] > '9')
        if (++poz == hg)
            fread (ch, 1, hg, f), poz = 0;

    while (ch[poz] >= '0' && ch[poz] <= '9')
    {
        x = x * 10 + ch[poz] - '0';
        if (++poz == hg)
            fread (ch, 1, hg, f), poz = 0;
    }
}
void citire()
{
    cit(n);cit(k);
}
void ciur()
{
    for(i=2;i<=MAX;i++)
     if(v[i]==0)
     {
      for(j=2*i;j<=MAX;j+=i)
        ++v[j];
      v[i]=1;
      }
     for (i=2;i<=MAX;i++)
      mat[v[i]][l[v[i]]++]=i;
}
void cb()
{
        for (st=0, dr=l[k]-1, x=0; st <= dr ; )
        {
           mij = st + ( (dr - st) >> 1 );
           if ( mat[k][mij] <= n )  st = mij + 1, x = mij;
           else if ( mat[k][mij] > n ) dr = mij-1;
        }
}
void afisare()
{
     if (mat[k][0]>n)
         fprintf(g,"0\n");
        else
        {
         cb();
         if ( mat[k][x] <= n ) fprintf(g,"%d\n",mat[k][x]);
          else fprintf(g,"0\n");
        }
}
void final()
{
    cit(t);
    while (t--)
    {
      citire();
      afisare();
    }
}
int main()
{
    ciur();
    final();
    return 0;
}