Cod sursa(job #2206511)

Utilizator memecoinMeme Coin memecoin Data 22 mai 2018 19:52:08
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 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 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;
}