Cod sursa(job #1328960)

Utilizator felixiPuscasu Felix felixi Data 28 ianuarie 2015 21:54:17
Problema GFact Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

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

struct STR {
    int v,p;
};

STR pr[100001];
int P,Q;
int pr_ind;

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

    for( int i = 1;  i <= pr_ind;  ++i )  pr[i].p *= Q;
}

int last = 1000000;

long long VAL( long long v, long long pr ) {
    long long 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].v ) < 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;
}