Cod sursa(job #2367118)

Utilizator EdyOnuEdy Onu EdyOnu Data 5 martie 2019 08:43:44
Problema Factorial Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.95 kb
// factorial.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

//#include "pch.h"
#include <utility>
#include <fstream>
#include <iostream>
#define RIGHT 100000000

using std::ifstream;
using std::ofstream;
using std::pair;
using std::cout;

int P;

inline void readData() {
	ifstream{ "fact.in" } >> P;
}

int getNumberOf5(long long x) {

	int nr = static_cast<long long>(x / 5);

	if (nr == 0) {
		return 0;
	}

	return nr + getNumberOf5(nr);
}

long long getN(int P) {

	if (P == 0) {
		return 1;
	}

	int left = 0, right = RIGHT;

	while (left <= right) {
	
		long long nr = (left + right) >> 1;
		long long rez = getNumberOf5(nr);

		if (rez == P) {

			while (nr && nr % 5 != 0) {
				--nr;
			}
			
			return nr;
		}

		rez < P ? left = nr + 1 : right = nr - 1;
	}

	return -1;
}

int main(){
	readData();
	ofstream{ "fact.out" } << getN(P) << '\n';
	return 0;
}