Cod sursa(job #3121536)

Utilizator daristyleBejan Darius-Ramon daristyle Data 13 aprilie 2023 20:35:56
Problema GFact Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <fstream>

using namespace std;

ifstream fin("gfact.in");
ofstream fout("gfact.out");

struct primeFactor {
		int fact;
		int pow;
};
const int PRIME_FACTORS_MAX = 9;
primeFactor v[PRIME_FACTORS_MAX];

int getPrimeFactors(int n, primeFactor v[]) {
	int m = 0, d = 3, p = 0;


	while(!(n & 1)){
		++p;
		n >>= 1;
	}
	if(p){
		v[m].fact = 2;
		v[m++].pow = p;
	}

	while(d * d <= n){
		p = 0;
		while(n % d == 0){
			++p;
			n /= d;
		}
		if(p){
			v[m].fact = d;
			v[m++].pow = p;
		}

		d += 2;
	}

	if(n > 1){
		v[m].fact = n;
		v[m++].pow = 1;
	}


	return m;
}

int Lagrange(long long n, int d) {
	long long p = d;
	int exp = 0;

	while(p <= n){
		exp += n / p;
		p *= d;
	}

	return exp;
}

bool check(long long x, primeFactor v[], int n) {
	int i = 0;
	while(i < n && Lagrange(x, v[i].fact) >= v[i].pow)
		++i;

	return i >= n;
}

int main() {
	int p, q;
	fin >> p >> q;

	int n = getPrimeFactors(p, v);
	for(int i = 0; i < n; ++i)
		v[i].pow *= q;

	//vom cauta binar cel mai mare numar X cu proprietatea ca A=pow(P,Q) nu divide X!
	long long b = v[n - 1].fact, e = (long long) p * q + 1, mid;//[b;e)
	while(e - b > 1){
		mid = (b + e) >> 1;
		if(!check(mid, v, n))
			b = mid;
		else
			e = mid;
	}

	fout << b + 1 << '\n';

	fin.close();
	fout.close();
	return 0;
}