Cod sursa(job #2800917)

Utilizator AndreiP15Andrei Enea AndreiP15 Data 14 noiembrie 2021 13:25:00
Problema GFact Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <fstream>

using namespace std;

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

long long p, q, i, pt[30], put[30], aux, k, ptb, d;
long long l, r, m, sol;

bool ok(long long b)
{
    for (i = 1; i <= k; i++)
    {
        d = pt[i];
        ptb = 0;
        while (d <= b)
        {
            ptb = ptb + b / d;
            d = d * pt[i];
        }
        if (ptb < put[i] * q)
            return false;
    }
    return true;
}

int main()
{
    f >> p >> q;
    if (p == 1)
        g << 0;
    else
    {
        aux = p;
        if (aux % 2 == 0)
        {
            pt[++k] = 2;
            put[k] = 1;
            aux /= 2;
            while (aux % 2 == 0)
            {
                put[k]++;
                aux /= 2;
            }
        }
        for (i = 3; i * i <= aux; i += 2)
        {
            if (aux % i == 0)
            {
                pt[++k] = i;
                put[k] = 1;
                aux /= i;
                while (aux % i == 0)
                {
                    put[k]++;
                    aux /= i;
                }
            }
        }
        if (aux > 1)
        {
            pt[++k] = aux;
            put[k] = 1;
        }
        l = 2;
        r = 60000000000000;
        while (l <= r)
        {
            m = (l + r) / 2;
            if (ok(m))
            {
                sol = m;
                r = m - 1;
            }
            else
                l = m + 1;
        }
        g << sol;
    }
    f.close();
    g.close();
    return 0;
}