Cod sursa(job #1385788)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 12 martie 2015 12:54:13
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include<fstream>

using namespace std;

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

const long long lim = 1LL << 50;

long long exponent( long long sol, int b ) {
    long long ans = 0, p = b;
    while ( p <= sol ) {
        ans += sol / p;
        p *= b;
    }
    return ans;
}
int main() {
    int p, q, x, y, aux, e;
    fin >> p >> q;
    aux = p;
    for( int i = 2; i * i <= p && aux > 1; ++ i ) {
        e = 0;
        while ( aux % i == 0 ) {
            aux /= i;
            ++ e;
        }

        if ( e > 0 ) {
            x = i; y = e;
        }
    }
    if ( aux > 1 ) {
        x = aux; y = 1;
    }

    y *= q;
    long long ans = 1;
    for( long long step = (lim >> 1LL); step > 0; step >>= 1 ) {
        if ( ans + step < lim && exponent( ans + step, x ) < y ) {
            ans += step;
        }
    }
    fout << ans + 1 << "\n";
    fin.close();
    fout.close();
    return 0;
}