Cod sursa(job #3252533)

Utilizator andrei257Andrei Enache andrei257 Data 29 octombrie 2024 20:17:44
Problema GFact Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
// https://www.infoarena.ro/problema/gfact

#include <bits/stdc++.h>
using namespace std;

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

int p, q, b, nr;

void dominantPrime()
{
	int n = p;
	vector<int> factor_list;
	unordered_map<int, int> factor_pow;
	factor_list.push_back(1), factor_pow[1]++;
	while (n > 1)
	{
		int f = 2;
		while (n % f != 0)
			f++;
		if (!factor_pow.count(f))
			factor_list.push_back(f);
		factor_pow[f]++;
		n /= f;
	}
	int bigPrime = 0;
	for (int i = 0; i < factor_list.size(); i++)
	{
		int power = pow(factor_list[i], factor_pow[factor_list[i]]);
		if (power > bigPrime)
		{
			b = factor_list[i];
			nr = factor_pow[factor_list[i]];
			bigPrime = power;
		}
	}
}

long long fact_b(const long long n)
{
	long long x = 0, i = b;
	while (true)
	{
		x += n / i;
		if (i * b > LONG_LONG_MAX)
			break;
		i *= b;
	}
	return x;
}

long long findSolution(const long long st, const long long dr)
{
	if (p == 1)
		return 0;
	const long long mid = (st + dr) / 2, val = fact_b(mid);
	if (val == nr * q * 1LL)
		return mid;
	if (val > nr * q * 1LL)
		return findSolution(st, mid - 1);
	return findSolution(mid + 1, dr);
}

int main()
{
	fin >> p >> q;
	dominantPrime();
	long long sol = findSolution(1, LONG_LONG_MAX);
	if (sol)
		sol -= sol % b;
	fout << sol;
	return 0;
}