Cod sursa(job #1155384)

Utilizator tudorv96Tudor Varan tudorv96 Data 26 martie 2014 21:15:13
Problema GFact Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.18 kb
#include <fstream>
#include <vector>
using namespace std;

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

typedef unsigned long long ull;

ull p, q;
vector <pair <ull, ull> > v;

bool go(ull x) {
    for (vector <pair <ull, ull> > :: iterator it = v.begin(); it != v.end(); ++it) {
        ull ans = 0, prod = it->first;
        while (prod <= x) {
            ans += x / prod;
            prod *= it->first;
        }
        if (ans < it->second)
            return 0;
    }
    return 1;
}

int main() {
    fin >> p >> q;
    int aux = p;
    if (p % 2 == 0) {
        v.push_back (make_pair (2, 0));
        while (p % 2 == 0) {
            v.back().second += q;
            p /= 2;
        }
    }
    for (ull i = 3; i * i <= aux && p != 1; i += 2)
        if (p % i == 0) {
            v.push_back (make_pair(p, 0));
            while (p % i == 0) {
                v.back().second += q;
                p /= i;
            }
        }
    if (p != 1)
        v.push_back (make_pair (p, q));
    ull sol = 1LL << 63;
    for (ull step = 1LL << 63; step; step >>= 1)
        if (go (sol - step))
            sol -= step;
    fout << sol;
}