Cod sursa(job #2149232)

Utilizator Alex_BubBuburuzan Alexandru Alex_Bub Data 2 martie 2018 13:41:53
Problema GFact Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <fstream>
#include <cmath>

using namespace std;

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

int nrz(int x, int f)
{
    int s = 0;
    while(x >= f) {
        x /= f;
        s += x;
    }
    return s;
}

int caut(int b, int p)
{
    int st, dr, ans;
    st = p * (b - 1);
    dr = p * b;

    while(st <= dr) {
        int m = st + (dr - st) / 2, n = nrz(m, b);

        if(n >= p) {
            ans = m;
            dr = m - 1;
        }
        else
            st = m + 1;
    }
    return ans;
}

int descompunere(int b, int e)
{
    int p = 0, ans = 1;
    while(!(b & 1)) {
        p++;
        b >>= 1;
    }
    if(p)
        ans = max(ans, caut(2, p * e));

    int d = 3;
    p = 0;
    while(d * d <= b && b > 1) {
        if(b % d) {
            d+=2;
            continue;
        }

        while(!(b % d)) {
            p++;
            b /= d;
        }

        ans = max(ans, caut(d, p * e));
        p = 0, d += 2;
    }

    if(b > 1)
        ans = max(ans, caut(b, e));

    return ans;
}

int main()
{
    int b, e;

    fin >> b >> e;

    fout << descompunere(b, e);

    return 0;
}