Cod sursa(job #2779870)

Utilizator PopoiuAndraPopoiu Andra-Stefania PopoiuAndra Data 5 octombrie 2021 12:36:08
Problema Divizori Primi Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>
#define N 1300005
using namespace std;

ifstream fin("divprim.in");
ofstream fout("divprim.out");

int T;
bool c[N];
int d[100005];
int a[7][400000];

int main()
{
    int i,j,ct=1;
    c[2]=1;
    for(i=3; i<=N; i+=2) c[i]=1;
    for(i=3; i*i<=N; i+=2)
        if(c[i]==1)
             for(j=i*i; j<=N; j+=2*i)
                c[j]=0;
    /// d[i] = nr de divizori primi
    for(i=2; i<=N; i+=2) d[i]=1;
    for(i=3; i<=N; ++i)
        if(c[i]==1)
            for(j=i; j<=N; j+=i) d[j]++;

    /// a[i][j] = matricea numerelor cu cel mult 7 divizori
    for(i=2; i<=N; ++i)
        if(d[i]<=7)
            a[d[i]][++a[d[i]][0]]=i;

    int x,y;
    fin>>T;
    for(i=1; i<=T; ++i)
    {
        fin>>x>>y;
        if(x<a[y][1]) fout<<0<<"\n";
        else if(x>a[y][a[y][0]]) fout<<a[y][ a[y][0] ]<<"\n";
        else
        {
            int st=1,dr=a[y][0],mij,poz;
            while(st<=dr)
            {
                mij=st+(dr-st)/2;
                if(x==a[y][mij]) poz=mij;
                else if(x<a[y][mij]) dr=mij-1;
                else poz=mij,st=mij+1;
            }
            fout<<a[y][poz]<<"\n";
        }
    }
    return 0;
}