Pagini recente » Cod sursa (job #1573764) | Cod sursa (job #929664) | Cod sursa (job #574443) | Cod sursa (job #1573391) | Cod sursa (job #479250)
Cod sursa(job #479250)
#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() {
v[2] = 1 ;
for ( i = 4; i <= MAX; i += 2 ) {
v[i] = 1 ;
}
for(i=3; i<=MAX; i+=2)
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;
}
int cb ( ) {
int i, cnt;
for (cnt = 1; cnt <= l[k]; cnt <<= 1) ;
for (i = 0; cnt; cnt >>= 1) {
if (i + cnt <= l[k] && mat[k][i + cnt] <= n) {
i += cnt;
}
}
return i;
}
void afisare() {
if (mat[k][0]>n)
fprintf(g,"0\n");
else {
x = 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;
}