Cod sursa(job #3169665)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 15 noiembrie 2023 19:00:33
Problema GFact Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.13 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;
ll div_jmek, putere_jmek;

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

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    cin >> P >> Q;
}
///-------------------------------------
inline bool ok(ll fact)
{
    ll put = div_jmek;
    ll cnt = 0;
    while (put <= fact)
    {
        cnt += fact / put;

        put *= div_jmek;
    }

    return (cnt >= putere_jmek);
}
///-------------------------------------
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);
        }
        
        div ++;
    }

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

    div_jmek = primi[primi.size() - 1];
    putere_jmek = puteri[primi.size() - 1];

    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;
}