Cod sursa(job #1861723)

Utilizator preda.andreiPreda Andrei preda.andrei Data 29 ianuarie 2017 11:20:14
Problema GFact Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>
#include <vector>

using namespace std;

typedef unsigned int uint;
typedef unsigned long long uint64;

vector<pair<int, uint>> Factors(int n, int p)
{
    vector<pair<int, uint>> factors;
    int div = 2;

    while (n > 1) {
        int exp = 0;
        while (n % div == 0) {
            n /= div;
            ++exp;
        }

        if (exp > 0) {
            factors.push_back({div, exp * p});
        }
        ++div;
    }
    return factors;
}

uint64 NumberOfFactors(uint64 num, int x)
{
    uint64 pow = x;
    uint64 factors = 0;

    while (pow <= num) {
        factors += num / pow;
        pow *= x;
    }
    return factors;
}

bool Divisible(uint64 num, const vector<pair<int, uint>> &factors)
{
    for (const auto &fact_pair : factors) {
        if (NumberOfFactors(num, fact_pair.first) < fact_pair.second) {
            return false;
        }
    }
    return true;
}

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

    int n, p;
    fin >> n >> p;

    auto a = Factors(n, p);
    uint64 pos = 0;
    uint64 pow = (1LL << 50);

    while (pow > 0) {
        if (!Divisible(pos + pow, a)) {
            pos += pow;
        }
        pow >>= 1;
    }

    fout << pos + 1 << "\n";
    return 0;
}