Cod sursa(job #1328986)

Utilizator felixiPuscasu Felix felixi Data 28 ianuarie 2015 22:11:36
Problema GFact Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

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

typedef long long I64;

const I64 INF = 0x3800000000000000;

struct STR {
    I64 v,p;
};

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

void desc( I64 N ) {
    I64 ind = 2;
    while( N > 1 && ind*ind <= N ) {
        I64 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;
}

I64 last = 1000000;

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

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

I64 BS( I64 st, I64 dr ) {
    if( st > dr )  return last;
    else {
        I64 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 );
    I64 Ans = BS( 1,2000000 );
    cout << Ans << '\n';
    return 0;
}