Cod sursa(job #1331732)

Utilizator chinmayiCobuz Cezara chinmayi Data 1 februarie 2015 03:47:22
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
/*Se da un numar intreg P. 
Sa se gaseasca cel mai mic numar natural strict pozitiv N pentru care N! are exact P cifre de 0 la sfarsit.*/
#include<iostream>
#include<fstream>
using namespace std;

unsigned long long number_of_zeroes(unsigned long long n)
{
	unsigned long long nr_of_0 = 0, power_of_5 = 5;
	while (n / power_of_5)
	{
		nr_of_0 += n / power_of_5;
		power_of_5 *= 5;
	}

	return nr_of_0;
}

int main()
{
	ifstream inFile("fact.in");
	ofstream outFile("fact.out");

		int P;
		inFile >> P;

		unsigned long long low = 1, high = 5000000000;

		while (low <= high)
		{
			unsigned long long middle = (low + high) / 2;
			if (number_of_zeroes(middle) < P)
				low = middle + 1;
			else
				high = middle - 1;
		}
		if (number_of_zeroes(low) == P)
			outFile << low << endl;
		else
			outFile << -1 <<endl;
}