Cod sursa(job #3169686)

Utilizator redstonegamer22Andrei Ion redstonegamer22 Data 15 noiembrie 2023 19:29:44
Problema Factorial Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;

#define int int64_t

#ifndef LOCAL
ifstream in("fact.in");
ofstream out("fact.out");
#define cin in
#define cout out
#endif // LOCAL

bool ok(int64_t fact, int factor, int put) {
	// numarul de aparitii ale factorului prim in fact!
	// = fact / factor + fact / factor^2 + ... + fact / factor^?

	int64_t sum = 0;
	while(fact > 0) {
		sum += fact / factor;
		fact /= factor;
	}

	return sum >= put;
}

int32_t main() {
	int p, q; p = 10;
	cin >> q;
	vector<pair<int, int>> factori_primi;

	// Pasul 1: Factorizarea
	for(int div = 2; div * div <= p; div++) {
		if(p % div == 0) {
			int putere = 0;
			while(p % div == 0) {
				putere++;
				p /= div;
			}
			factori_primi.push_back({div, putere});
		}
	}
	if(p > 1) {
		factori_primi.push_back({p, 1});
	}

	// Pasul 2: Cautarea binara
	// int64_t st = 0, dr = 1LL * factori_primi.back().first * factori_primi.back().second() * q + 1;

	int64_t val = (1LL << 60) - 1;
	for(int64_t step = (1LL << 59); step > 0; step >>= 1) {
		// Verificam daca val - step este solutie
		// Daca da, atunci val -= step

		bool este_ok = true;
		for(auto pr : factori_primi) {
			este_ok &= ok(val - step, pr.first, pr.second * q);
		}

		if(este_ok) {
			val -= step;
		}
	}

	if(val == 0) val++;

	cout << val << '\n';
}