Cod sursa(job #2086563)

Utilizator vladstanciuVlad Stanciu vladstanciu Data 12 decembrie 2017 10:44:49
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>

using namespace std;

const int ND=10, L=45;
int d[ND], e[ND], nd, p, q;

ifstream in("gfact.in");
ofstream out("gfact.out");

void desc(int n)
{
    int i=2;
    while(i*i<=n)
    {
        if(n%i==0)
        {
            d[nd]=i;
            while(n%i==0)
            {
                e[nd]++;
                n/=i;
            }
            nd++;
        }
        i++;
    }
    if(n!=1)
    {
        d[nd]=n;
        e[nd]=1;
        nd++;
    }
}

long long putere(long long x, int y)
{
    long long nr=0;
    while(x>=y)
    {
        nr+=x/y;
        x/=y;
    }
    return nr;
}

bool divizibil(long long n)
{
    for(int i=0; i<nd; i++)
    {
        if(putere(n, d[i])< e[i]*q) return false;
    }
    return true;
}

int main()
{

    long long r;
    long long pas;
    in>>p>>q;
    desc(p);
    pas=1LL<<L;
    r=0;
    while(pas!=0)
    {
        if(!divizibil(r+pas)) r+=pas;
        pas/=2;
    }
    r++;
    out<<r;
    return 0;
}