Cod sursa(job #3204451)

Utilizator TeodorVTeodorV TeodorV Data 16 februarie 2024 19:05:18
Problema GFact Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <bits/stdc++.h>

using namespace std;

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

vector<pair<int, int>> fact;
void initFact(int x)
{
    fact.reserve(10);
    int exp=0;
    while(x%2==0)
    {
        exp++;
        x/=2;
    }
    if(exp>0)
        fact.push_back(make_pair(2, exp));
    for(int d=3; d*d<=x; d+=2)
    {
        exp=0;
        while(x%d==0)
        {
            exp++;
            x/=d;
        }
        if(exp>0)
            fact.push_back(make_pair(d, exp));
    }
    if(x>1)
        fact.push_back(make_pair(x, 1));
}

bool verifDiv(int n)
{
    for(unsigned int i=0; i<fact.size(); i++)
    {
        int cnt=0;
        while(fact[i].first<=n)
        {
            cnt+=(n/=fact[i].first);
            if(cnt>=fact[i].second)
                break;
        }
        if(cnt<fact[i].second)
            return 0;
    }
    return 1;
}

long long cb()
{
    long long st=1,dr=2e9,mij,rez=-1;
    while(st<=dr)
    {
        mij=(st+dr)/2;
        if(verifDiv(mij))
        {
            rez=mij;
            dr=mij-1;
        }
        else st=mij+1;
    }
    return rez;
}

int main()
{
    int p,q;
    fin>>p>>q;
    initFact(p);
    for(unsigned int i=0; i<fact.size(); i++)
    {
        fact[i].second*=q;
        //cout<<fact[i].first<<' '<<fact[i].second<<'\n';
    }
    fout<<cb();
    return 0;
}