Cod sursa(job #3330701)

Utilizator CalinPaun29Paun Calin CalinPaun29 Data 21 decembrie 2025 13:17:48
Problema GFact Scor 85
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb
//#pragma GCC optimize("O3, Ofast, unroll-loops")
#include <bits/stdc++.h>
#define int long long
using namespace std;

signed main()
{
    ifstream cin("gfact.in");
    ofstream cout("gfact.out");
    int p, q;
    cin>>p>>q;
    map<int, int> mp;
    while(p%2==0)
    {
        mp[2]+=q;
        p/=2;
    }
    int div=3;
    while(p>1 && div<=p)
    {
        while(p%div==0)
        {
            mp[div]+=q;
            p/=div;
        }
        div+=2;
    }
    int l=-1, r=6*1e14, mid;
    while(l<r-1)
    {
        mid=(l+r)/2;
        int ok2=0;
        for(auto it:mp)
        {
            int x=it.first, y=it.second;
            int nr=0, ok=0, cc=x;
            while(nr<y)
            {
                if(mid/cc==0)
                {
                    ok=1;
                    break;
                }
                nr+=mid/cc;
                cc*=x;
            }
            if(ok==1)
            {
                ok2=1;
                break;
            }
        }
        if(ok2==0)
            r=mid;
        else
            l=mid;
    }
    cout<<r;
    return 0;
}