Pagini recente » Cod sursa (job #1863458) | Cod sursa (job #3155132) | Cod sursa (job #2245122) | Cod sursa (job #2378784) | Cod sursa (job #778285)
Cod sursa(job #778285)
//============================================================================
// Name : Factorial.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include <fstream>
using namespace std;
unsigned int get_zero_count(int n) {
unsigned int sum = 0;
for(unsigned int i = 5; i <= n; i *= 5)
sum += n / i;
return sum;
}
unsigned int bsearch(unsigned int target) {
unsigned int low = 1, high = 100000000, mid;
while(low <= high) {
mid = (low + high) / 2;
unsigned int 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() {
int p;
ifstream fin("fact.in");
ofstream fout("fact.out");
fin >> p;
if(p == 0) {
fout << 1;
return 0;
}
unsigned int k = bsearch(p);
if(k % 5)
fout << k - k % 5;
else
fout << k;
fin.close();
fout.close();
return 0;
}