Cod sursa(job #3234107)

Utilizator RaresStanStan Rares RaresStan Data 6 iunie 2024 15:05:27
Problema GFact Scor 65
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <bits/stdc++.h>

#define pb push_back
#define ll long long
#define NMAX 100005
using namespace std;
ll p, q;
vector<pair<ll, ll>> d;

bool f(ll x) {
    ll s = 0;
    for (auto i: d) {
        ll a = i.first;
        while (a <= x) {
            s += x / a;
            a *= i.first;
        }
        if (s < i.second * q)
            return false;
    }
    return true;
}

int main() {
    ifstream cin("gfact.in");
    ofstream cout("gfact.out");
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    cin >> p >> q;
    ll x = p;
    if (x % 2 == 0) {
        ll c = 0;
        while (x % 2 == 0) {
            x /= 2;
            c++;
        }
        d.pb({2, c});
    }
    for (ll i = 3; i * i <= p; i += 2) {
        ll power = 0;
        if (x % i == 0) {
            while (x % i == 0) {
                x /= i;
                power++;
            }
            d.pb({i, power});
        }
    }
    if (x != 1)
        d.pb({x, 1});
    ll st = 1, dr = 1ll * 1000000000 / 2 * 1000000000, r = 0;
    while (st <= dr) {
        ll mid = (st + dr) / 2;
        if (f(mid))
            dr = mid - 1, r = mid;
        else
            st = mid + 1;
    }
    cout << r;
    return 0;
}