Cod sursa(job #2550298)

Utilizator horiacoolNedelcu Horia Alexandru horiacool Data 18 februarie 2020 18:12:11
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <iostream>
#include <fstream>
#include <limits>
using namespace std;

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

#define LL long long int
#define ULONG_MAX 4 * 4294967295

int main() {
	LL P;
	LL N = 0, countP = 0, pow5 = 1;

	fin >> P;

	while (N < ULONG_MAX) {
		N += 4 * pow5;
		countP += (pow5 - 1);
		pow5 *= 5;
	}

	pow5 /= 5;
	for (; pow5 > 0; pow5 /= 5) {
		if (countP == P)
			break;

		if (countP - (pow5 - 1) / 4 >= P) {
			countP -= (pow5 - 1) / 4;
			N -= pow5;

			if (countP - (pow5 - 1) / 4 >= P) {
				countP -= (pow5 - 1) / 4;
				N -= pow5;

				if (countP - (pow5 - 1) / 4 >= P) {
					countP -= (pow5 - 1) / 4;
					N -= pow5;

					if (countP - (pow5 - 1) / 4 >= P) {
						countP -= (pow5 - 1) / 4;
						N -= pow5;
					}
				}
			}
		}
	}

	if (P != 0) {
		if (countP != P) {
			fout << -1;
		} else {
			N -= 4;
			fout << N;
		}
	} else {
		fout << 1;
	}

	return 0;
}