Cod sursa(job #2088725)

Utilizator Davla2Stancu Vlad Davla2 Data 15 decembrie 2017 19:15:33
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>

using namespace std;

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

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

long long putere(long long n, int d)
{
    long long nr=0;
    //out << n << "! are ";
    while(n>=d) nr += (n/=d);
    //out << nr << d << "-uri\n";
    return nr;
}

bool sedivide(long long n)
{
    //out << "nd = " << nd << "\n";
    for(int i=1; i<=nd; i++)
    {
        if(putere(n,d[i]) < e[i]*q)
            return false;
    }
    return true;
}

int main()
{
    in>>p>>q;
    int i=2;
    while(i*i<=p)
    {
        if(p%i==0)
        {
            d[++nd]=i;
            while(p%i==0)
            {
                e[nd]++;
                p/=i;
            }
        }
        i++;
    }
    if(p!=1)
    {
        d[++nd]=p;
        e[nd]=1;
    }
    /*
    for (int i = 1; i <= nd; i++)
    {
        out << d[i] << "^" << e[i] << " ";
    }
    */
    long long r=0,pas=1LL<<L;
    while(pas!=0)
    {
        if(!sedivide(r+pas))
        {
            r+=pas;
        }
        pas/=2;
    }
    r++;
    out<<r;
    return 0;
}