Cod sursa(job #2793651)

Utilizator cristia_razvanCristia Razvan cristia_razvan Data 3 noiembrie 2021 20:54:17
Problema GFact Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.14 kb
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define pb push_back

const string filename = "gfact";

ifstream fin(filename + ".in");
ofstream fout(filename + ".out");

vector<pair<ll, ll> > fct;

bool howMany(ll x)
{
    for (auto v : fct)
    {
        ll nr = 0, p = v.first;
        while (p <= x)
        {
            nr += x / p;
            p *= v.first;
        }
        if (nr < v.second)
            return false;
    }
    return true;
}

int main () {

    int p, q;
    fin >> p >> q;

    int d = 2;
    while (p > 1)
    {
        int e = 0;
        while (p % d == 0)
            p /= d, ++e;
        if (e)
            fct.push_back({d, e * q});
        if (d == 2)
            d = 3;
        else d += 2;
        if (d * d > p)
            d = p;
    }

    ll st = 0, dr = 1e16L, ans = dr;

    while (st <= dr)
    {
        ll mid = (st + dr) >> 1;
        if (howMany(mid))
        {
            ans = mid;
            dr = mid - 1;
        }
        else st = mid + 1;
    }

    fout << ans << '\n';

    fin.close();
    fout.close();
    return 0;
}