Pagini recente » Cod sursa (job #2299190) | Cod sursa (job #959563) | Cod sursa (job #453486) | Cod sursa (job #2765290) | Cod sursa (job #2206511)
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;
int n;
int divisorsOf5(int x) {
int result = 0;
while (x > 0 && x % 5 == 0) {
x /= 5;
result++;
}
return result;
}
int greatestMultiple(int x) {
int result = 5;
while (x > result * 5) {
result *= 5;
}
return result;
}
int zeroCount(int x) {
x -= x % 5;
int result = 0;
while (x > 0) {
int g = greatestMultiple(x);
if (x % g == 0) {
result += divisorsOf5(x);
x -= 5;
}
else {
result += (x - g) / (g / 5);
x -= x % g;
}
}
return result;
}
int find(int l, int r, int x) {
int mid = (l + r) / 2;
int value = zeroCount(mid);
if (value == x) {
return mid - mid % 5;
} else if (value > x) {
return find(l, mid - 1, x);
} else {
return find(mid + 1, r, x);
}
}
int main() {
freopen("fact.in", "r", stdin);
freopen("fact.out", "w", stdout);
scanf("%d", &n);
int upperBound = 100;
while (zeroCount(upperBound) < n) {
upperBound *= 2;
}
int x = find(1, upperBound, n);
printf("%d", x ? x : 1);
return 0;
}