Pagini recente » Cod sursa (job #2547373) | Cod sursa (job #2650579) | Cod sursa (job #1391086) | Cod sursa (job #253972) | Cod sursa (job #778290)
Cod sursa(job #778290)
//============================================================================
// Name : Factorial.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <fstream>
using namespace std;
typedef unsigned long long ull;
ull get_zero_count(ull n) {
ull sum = 0;
for(ull i = 5; i <= n; i *= 5)
sum += n / i;
return sum;
}
ull bsearch(ull target) {
ull low = 1, high = 300000000, mid;
while(low <= high) {
mid = (low + high) / 2;
ull zero_count = get_zero_count(mid);
if(zero_count > target)
high = mid - 1;
else if(zero_count < target)
low = mid + 1;
else
return mid;
}
return -1;
}
int main() {
ull p;
ifstream fin("fact.in");
ofstream fout("fact.out");
fin >> p;
if(p == 0) {
fout << 1;
return 0;
}
ull k = bsearch(p);
if(k % 5)
fout << k - k % 5;
else
fout << k;
fin.close();
fout.close();
return 0;
}