Cod sursa(job #1329482)

Utilizator PikachuPikachu Pikachu Data 29 ianuarie 2015 16:04:22
Problema GFact Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

vector <int> fact, expp;

void desc(int n, int q) {
	int lim, d, e;
	d = 2;
	//lim = (int) sqrt((double) n);
	while(n > 1 && d * d <= n) {
		e = 0;
		while(n % d == 0) {
			n /= d;
			++ e;
		}
		if(e) {
			fact.insert(fact.end(), d);
			expp.insert(expp.end(), e * q);
		}
		++ d;
	}
	if(n > 1) {
		fact.insert(fact.end(), n);
		expp.insert(expp.end(), q);
	}
}

bool ok(int n, int p, int e) {
	int pp = 0;
	while(n) {
		pp = pp + n / p;
		n /= p;
	}
	if(pp >= e)
		return 1;
	return 0;
}

int bin(int p, int e) {
	int med, last = -1, st = 1, dr = (1LL << 31) - 1;
	while(st <= dr) {
		med = st + (dr - st) / 2;
		if(ok(med, p, e)) {
			last = med;
			dr = med - 1;
		} else {
			st = med + 1;
		}
	}
	return last;
}

int main() {
	freopen("gfact.in", "r", stdin);
	freopen("gfact.out", "w", stdout);

	int p, q, rasp = 0, n;
	scanf("%d%d", &p, &q);

	desc(p, q);

	n = fact.size();

	for(int i = 0; i < n; ++ i) {
		rasp = max(rasp, bin(fact[i], expp[i]));
	}

	printf("%d\n", rasp);

	return 0;
}