Cod sursa(job #2661597)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 22 octombrie 2020 12:35:08
Problema GFact Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
	
#include <bits/stdc++.h>
#define int long long

using namespace std;
 
ifstream fin("gfact.in");
ofstream fout("gfact.out");

int32_t main() {
    fin.sync_with_stdio(false);
    fout.sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);
    int P, Q;
    fin >> P >> Q;
    vector < pair < int , int > > primes;
    int d = 2;
    while(P > 1) {
        int e = 0;
        while(P % d == 0) {
            P /= d;
            ++e;
        }
        if(e) 
            primes.emplace_back(d, e * Q);
        if(d == 2)
            d = 3;
        else
            d += 2;
        if(d * d > P)
            d = P;
    }
    auto check = [&](int x) {
        for(auto& v : primes) {
            int e = 0, p = v.first;
            while(p <= x) {
                e += x / p;
                p *= v.first;
            }
            if(e < v.second)
                return false;
        }
        return true;
    };
    int st = 0, dr = 1e16L, ans = dr;
    while(st <= dr) {
        int mid = (st + dr) >> 1;
        if(check(mid)) {
            ans = mid;
            dr = mid - 1;
        }
        else 
            st = mid + 1;
    }
    fout << ans;
}