Cod sursa(job #169224)

Utilizator scvalexAlexandru Scvortov scvalex Data 1 aprilie 2008 14:09:44
Problema GFact Scor 75
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

int P, Q;
vector<pii> T;

int putere(int x, int t) {
	int r(0);
	int aux = t;
	while (aux <= x) {
		r += x / aux;
		aux *= t;
	}
	return r;
}

int main(int argc, char *argv[]) {
	ifstream fin("gfact.in");
	fin >> P >> Q;
	fin.close();

	int aux = P;
	int t(0);
	while (aux % 2 == 0) {
		++t;
		aux /= 2;
	}
	if (t)
		T.push_back(make_pair(2, t));
	int i = 3;
	while (i <= aux) {
		t = 0;
		while (aux % i == 0) {
			++t;
			aux /= i;
		}
		if (t)
			T.push_back(make_pair(i, t));
		i += 2;
	}

	int m = -1;
	for (vector<pii>::iterator it = T.begin(); it != T.end(); ++it) {
		int t = it->first;
		int f = it->second * Q;
		//cout << t << " " << f << endl;

		int p = 1;
		int r = f;
		while (p != r) {
			//cout << p << " " << r << endl;
			int q = (p + r) / 2;
			if (putere(q * t, t) < f)
				p = q + 1;
			else
				r = q;
		}

		//cout << p*t << endl;
		if (m < p*t)
			m = p*t;
	}

	ofstream fout("gfact.out");
	fout << m << endl;
	fout.close();

	return 0;
}