Cod sursa(job #2206496)

Utilizator memecoinMeme Coin memecoin Data 22 mai 2018 19:28:34
Problema Factorial Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#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 zeroCount(int x) {
	x = x - x % 5;
	int result = 0;
	while (x > 0) {
		result += divisorsOf5(x);
		x -= 5;
	}
	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;
}