Cod sursa(job #1328687)

Utilizator felixiPuscasu Felix felixi Data 28 ianuarie 2015 17:52:30
Problema GFact Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

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

int P,Q;
int pr[10000], pr_ind;

void desc( int N ) {
    int ind = 2;
    while( N > 1 && ind*ind <= N ) {
        int ok = 0;
        while( N % ind == 0 ) {
            ok = 1;
            N/= ind;
        }
        if( ok )  pr[ ++pr_ind ] = ind;
    }
    if( N > 1 ) pr[ ++pr_ind ] = N;
}

int last = 1000000;

int VAL( int v, int pr ) {
    int ans = 0;
    int cop = pr;
    while( v/pr > 0 ) {
        ans += v/pr;
        pr*= cop;
    }
    return ans;
}

bool OK( int A ) {
    bool ok = 1;
    for( int i = 1;  i <= pr_ind;  ++i ) {
        if( VAL( A,pr[i] ) < Q ) {
            ok = 0;
            break;
        }
    }
    return ok;
}

int BS( int st, int dr ) {
    if( st > dr )  return last;
    else {
        int mid = (st+dr)/2;
        if( OK(mid) ) {
            last = mid;
            return BS( st, mid-1 );
        }
        else {
            return BS( mid+1, dr );
        }
    }
}

int main() {
    cin >> P >> Q;
    desc( P );
    int Ans = BS( 1,1000000 );
    cout << Ans << '\n';
    return 0;
}