Cod sursa(job #2556029)

Utilizator As932Stanciu Andreea As932 Data 24 februarie 2020 17:16:28
Problema GFact Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <vector>
#define ll long long
using namespace std;
ifstream fin("gfact.in");
ofstream fout("gfact.out");

ll p,q;
vector < pair <ll,ll> > v;

bool ok(long long nr)
{
    for(ll i=0;i<v.size();i++)
    {
        ll divz=v[i].first;
        ll ans=0;
        while(divz<=nr)
        {
            ans+=nr/divz;
            divz*=v[i].first;
        }
        if(ans<v[i].second*q)
            return false;
    }
    return true;
}

int main()
{
    fin>>p>>q;

    ll d=2,poww;
    while(p>1)
    {
        poww=0;
        while(p%d==0)
        {
            p/=d;
            poww++;
        }
        if(poww!=0)
            v.push_back({d,poww});
        if(d==2)
            d++;
        else
            d+=2;
        if(p>1 && d*d>p)
            d=p;
    }

    ll st=1,dr=2000000000,sol=0;
    while(st<=dr)
    {
        ll mij=(st+dr)/2;
        if(ok(mij))
        {
            sol=mij;
            dr=mij-1;
        }
        else
            st=mij+1;
    }
    fout<<sol;

    return 0;
}