Cod sursa(job #846582)

Utilizator vld7Campeanu Vlad vld7 Data 2 ianuarie 2013 14:46:27
Problema GFact Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <algorithm>

using namespace std;

ifstream f("gfact.in");
ofstream g("gfact.out");

long long P, poz = 1, sol;
int Q, put, cnt;

void solve(long long fact, int putere)
{
	long long limit = fact, poz = 1;
	long long need = 1LL * putere * Q;
	cnt = 1;
	int pow = 1;
	
	while (cnt < need) {
		long long NR = (1LL * limit * fact - fact - limit) / fact;	// cate nr sunt divizibile cu fact din (limit, limit * fact)
		if (cnt + NR > need) {
			poz += need - cnt;
			cnt = need;
		} else {
			cnt += NR;
			poz += NR;
			limit *= fact;
			pow++;
			if (cnt < need)
				cnt += pow, poz++;
		}
	}
	long long rez = 1LL * fact * poz;
	sol = max(sol, rez);
}

int main()
{
	f >> P >> Q;
	long long aux = P, fact = 2;
	
	while (aux > 1) {
		put = 0;
		while (aux % fact == 0) {
			aux /= fact;
			put++;
		}
		if (put)
			solve(fact, put);
		fact++;
	}
	
	g << sol << '\n';
	
	return 0;
}