Pagini recente » Sandbox (cutiuţa cu năsip) | Cod sursa (job #2626264) | Cod sursa (job #1250452) | Cod sursa (job #2750411) | Cod sursa (job #1861727)
#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 << 40);
while (pow > 0) {
if (!Divisible(pos + pow, a)) {
pos += pow;
}
pow >>= 1;
}
fout << pos + 1 << "\n";
return 0;
}