Cod sursa(job #475151)

Utilizator darrenRares Buhai darren Data 6 august 2010 11:13:16
Problema GFact Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>

using namespace std;

typedef long long i64;

const i64 MAX = 1LL << 50;

void Read();
void Solve();
bool Test(i64 x);
void Write();

i64 p, q, lf;
i64 res;

int main()
{
	Read();
	Solve();
	Write();
}

void Read()
{
	ifstream fin("gfact.in");
	fin >> p >> q;
	fin.close();
}

void Solve()
{
	int cl = p, exlf = 1;
	for (int i = 2; i * i <= p; ++i)
	{
		if (cl % i == 0)
		{
			lf = i;
			exlf = 0;
			while (cl % i == 0)
			{
				cl /= i;
				++exlf;
			}
		}
		if (i != 2) ++i;
	}
	if (cl != 1)
	{
		lf = cl, exlf = 1;
	}
	q *= exlf;

	i64 step;
	for (step = 1; (step << 1) <= MAX; step <<= 1);
	for (res = MAX + 1; step; step >>= 1)
		if (res - step >= 1 && Test(res - step))
			res -= step;
}

bool Test(i64 x)
{
	i64 now = lf, ex = 0;
	while (now <= x)
	{
		ex += x / now;
		now *= lf;
	}
	return (ex >= q);
}

void Write()
{
	ofstream fout("gfact.out");
	fout << res;
	fout.close();
}