Cod sursa(job #2812391)

Utilizator andreiiorgulescuandrei iorgulescu andreiiorgulescu Data 4 decembrie 2021 14:36:22
Problema GFact Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.04 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,p,factmin;
vector<int>f;
vector<int>e;

int number(int k,int x)
{
    int sol = 0;
    for (int i = x; i <= k; i *= x)
        sol += k / i;
    return sol;
}

int fm(int x,int y)
{
    int k = 1e6,pas = 1 << 19;
    while (pas != 0)
    {
        if (k - pas > 0)
            if (number(k - pas,x) >= y)
                k -= pas;
        pas /= 2;
    }
    return k;
}

int main()
{
    in >> n >> p;
    for (int i = 2; i <= sqrt(n); i++)
    {
        if (n % i == 0)
        {
            f.push_back(i);
            int exp = 0;
            while (n % i == 0 and n != 1)
            {
                exp++;
                n /= i;
            }
            e.push_back(exp);
        }
    }
    if (n != 1)
    {
        f.push_back(n);
        e.push_back(p);
    }
    for (int i = 0; i < f.size(); i++)
        factmin = max(factmin,fm(f[i],e[i]));
    out << factmin;
    return 0;
}