Cod sursa(job #2843728)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 2 februarie 2022 20:39:12
Problema GFact Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <bits/stdc++.h>
/// TONI BO$$ was here
/// #MLC

using namespace std;
int b[15], e[15], m;

int bs(int x)
{
    for(int i = 1; i <= m; i++)
    {
        long long pb = b[i];
        int tot = 0;
        while(pb <= x)
            tot += x / pb, pb *= b[i];
        if(tot < e[i])
            return 0;
    }
    return 1;
}

int main()
{
    int p, q, d, ex, pas, rez;
    freopen("gfact.in","r",stdin);
    freopen("gfact.out","w",stdout);
    scanf("%d%d", &p, &q);
    d = 2;
    while(d * d <= p)
    {
        ex = 0;
        while(!(p % d))
            ex++, p /= d;
        if(ex) ++m, b[m] = d, e[m] = ex * q;
        d++;
    }
    if(p - 1)
       ++m, b[m] = p, e[m] = q;
    pas = 1 << 30;
    rez = 0;
    while(pas)
    {
        if(!bs(rez + pas))
            rez += pas;
        pas /= 2;
    }
    printf("%d", rez + 1);

    return 0;
}