Cod sursa(job #1701764)

Utilizator StarGold2Emanuel Nrx StarGold2 Data 14 mai 2016 01:18:10
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include <bits/stdc++.h>

const int DIM = 1 << 5;
using namespace std;

int N, P, Q, st, dr, mid, fact, expon; long long answer;

inline bool good_value( long long val, int fact, int expon ) {
    int cnt = 0; long long prod = fact;

    while( 1 ) {
        cnt += val / prod;

        if( val / prod < fact )
            break;
        else
            prod *= fact;
    }

    return cnt >= expon;
}

inline void solve( int i ) {
    if( P % i != 0 ) return;

    fact = i; expon = 0;

    while( P % i == 0 ) {
        expon += Q;
        P /= i;
    }

    st = 1, dr = expon;
    while( st <= dr ) {
        mid = st + ( dr - st ) / 2;

        if( good_value( mid * 1LL * fact, fact, expon ) )
            dr = mid - 1;
        else
            st = mid + 1;
    }

    answer = max( answer, st * 1LL * fact );
    return;
}

int main() {

    FILE *input_file  = fopen( "gfact.in" , "r" );
    FILE *output_file = fopen( "gfact.out", "w" );

    fscanf( input_file, "%d %d", &P, &Q );

    for( int i = 2; i * i <= P; i ++ )
        solve( i );

    if( P != 1 )
        solve( P );

    fprintf( output_file, "%lld\n", answer );

    return 0;
}