Cod sursa(job #475142)

Utilizator darrenRares Buhai darren Data 6 august 2010 10:57:27
Problema GFact Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <fstream>

using namespace std;

const int MAX = 1 << 28;

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

int p, q, lf;
int res;

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

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

void Solve()
{
	int cl = p, exlf;
	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;

	int 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(int x)
{
	int 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();
}