Cod sursa(job #3169655)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 15 noiembrie 2023 18:56:07
Problema GFact Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.16 kb
#ifndef LOCAL
    #pragma GCC optimize("Ofast")
#endif

#ifdef LOCAL
    #define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
using ll = long long;
using ci = const int;
using cll = const long long;

using namespace std;

const int LOG = 46;
/*******************************/
// INPUT / OUTPUT

#ifndef LOCAL
    ifstream in("gfact.in");
    ofstream out("gfact.out");
    #define cin in
    #define cout out
#endif
/*******************************/
/// GLOBAL DECLARATIONS

int P, Q;
ll ans;

vector <int> primi, puteri;
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    cin >> P >> Q;
}
///-------------------------------------
inline bool ok(ll fact)
{
    ll put;
    ll cnt, div;
    for (int i = 0 ; i < primi.size() ; ++ i)
    {
        div = primi[i];
        cnt = 0;

        put = div;
        while (put <= fact)
        {
            cnt += fact / put;

            put *= div;
        }

        if (cnt < puteri[i])
            return false;
    }

    return true;
}
///-------------------------------------
inline void Solution()
{
    int div = 2;

    int nr = P, cnt = 0;
    while (div * div <= nr)
    {
        if (nr % div == 0)
        {
            cnt = 0;
            primi.push_back(div);

            while (nr % div == 0)
            {
                nr /= div;
                cnt ++;
            }
            puteri.push_back(cnt * Q);
        }
    }

    if (nr != 1)
    {
        primi.push_back(nr);
        puteri.push_back(Q);
    }

    
    ll sol = 0, step = (1LL << LOG);
    
    while (step)
    {
        if (!ok(sol + step))
            sol += step;
        
        step >>= 1;
    }
    
    if (!ok(sol))
        sol ++;
    
    ans = sol;
}
///-------------------------------------
inline void Output()
{
    cout << ans;
}
///-------------------------------------
int main()
{
    #ifndef LOCAL
        ios::sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
    #endif
    ReadInput();
    Solution();
    Output();
    return 0;
}