Cod sursa(job #3253405)

Utilizator andrei257Andrei Enache andrei257 Data 2 noiembrie 2024 15:18:03
Problema GFact Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 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, f = 2, rec = 0;
	while (n > 1 && f * f <= p)
	{
		int start = n, put = 0;
		while (n % f == 0)
			put++, n /= f;
		if (start / n > rec)
			rec = start / n, b = f, nr = put;
		f++;
	}
	if (f * f > p && n != 1)
		b = n, nr = 1;
}

long long fact_b(const long long n)
{
	long long x = 0, i = b;
	while (true)
	{
		x += n / i;
		if (i * b > INT_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;
}