Cod sursa(job #2791408)

Utilizator Stefan_GhinescuGhinescu Stefan-George Stefan_Ghinescu Data 30 octombrie 2021 14:27:13
Problema GFact Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <fstream>

using namespace std;

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

#define ull unsigned long long

const ull BIG = (1LL<<60);

ull p, q, nrfact;

struct prim
{
    ull baza, exp;
};

prim v[200];

void descompunere(ull n)
{
    if (n % 2 == 0)
    {
        v[nrfact].baza = 2;
        while (n % 2 == 0)
        {
            v[nrfact].exp++;
            n /= 2;
        }
        ++nrfact;
    }
    for (ull i = 3; i * i <= n; i += 2)
    {
        if (n % i == 0)
        {
            v[nrfact].baza = i;
            while (n % i == 0)
            {
                v[nrfact].exp++;
                n /= i;
            }
            ++nrfact;
        }
    }
    if (n > 1)
    {
        v[nrfact].baza = n;
        v[nrfact].exp = 1;
        ++nrfact;
    }
}

ull idfk(ull n, ull d)
{
    ull s = 0;
    for (ull i = d; n >= i; i *= d)
        s += n / i;
    return s;
}

bool f(ull n)
{
    for (ull i = 0; i < nrfact; i++)
    {
        if (idfk(n, v[i].baza) < q * v[i].exp)
        {
            return 0;
        }
    }
    return 1;
}

#if 0
ull bs()
{
    ull st = 0, dr = BIG, mij;
    while (st < dr)
    {
        mij = (st + dr) / 2;
        cout << st << "  AA\n";
        if (f(mij))
        {
            dr = mij;
        }
        else
        {
            st = mij + 1;
        }
    }
    return dr;
}
#endif

ull bs()
{
    ull rez = 0, pas = (1LL<<60);
    while (pas)
    {
        if (!f(rez + pas))
        {
            rez += pas;
        }
        pas >>= 1;
    }
    return rez + 1;
}

int main()
{
    fin >> p >> q;
    descompunere(p);
    fout << bs();
    return 0;
}