Pagini recente » Cod sursa (job #219874) | Cod sursa (job #691187) | Cod sursa (job #2473518) | Cod sursa (job #738483) | Cod sursa (job #1331732)
/*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;
}