Cod sursa(job #2793277)

Utilizator Andrei_ierdnANeculau Rares-Andrei Andrei_ierdnA Data 3 noiembrie 2021 13:36:08
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, fact[30], expfact[30], aux, k, expb, d;
long long l, r, m, sol;

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

int main()
{
    f >> p >> q;
    if (p == 1)
        g << 0;
    else
    {
        aux = p;
        if (aux % 2 == 0)
        {
            fact[++k] = 2;
            expfact[k] = 1;
            aux /= 2;
            while (aux % 2 == 0)
            {
                expfact[k]++;
                aux /= 2;
            }
        }
        for (i = 3; i * i <= aux; i += 2)
        {
            if (aux % i == 0)
            {
                fact[++k] = i;
                expfact[k] = 1;
                aux /= i;
                while (aux % i == 0)
                {
                    expfact[k]++;
                    aux /= i;
                }
            }
        }
        if (aux > 1)
        {
            fact[++k] = aux;
            expfact[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;
}