Cod sursa(job #561273)

Utilizator sebii_cSebastian Claici sebii_c Data 19 martie 2011 15:18:25
Problema Factorial Scor 25
Compilator c Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <stdio.h>
#define INFILE "fact.in"
#define OUTFILE "fact.out"

int P;

int digits(int N)
{
	int dig = 0;
	do {
		dig++;
		N /= 10;
	} while (N);
	return dig;
}

int factorial(int N)
{
	int i, j;
	int count = 0;
	for (i=5; i<=N; i+=5) {	
			j = i;
			while (!(j%5)) {
				j /= 5;
				count++;
			}
		}
	return count;
}

int cauta(int target, int low, int high)
{
	if (low > high)
		return -1;
	int mid = (low+high)/2;
	int f = factorial(mid);
	if (f == target)
		return mid;
	else if (target < f)
		cauta(target, low, mid-1);
	else
		cauta(target, mid+1, high);
}
	

int main()
{
	freopen(INFILE, "r", stdin);
	freopen(OUTFILE, "w", stdout);
	scanf("%d", &P);
	if (P == 0) {
		printf("1");
		return 0;
	}
	int low = 0;
	int high = 100000; 
	int result = cauta(P, low, high);
	if (result == -1) {
		printf("-1");
		return 0;
	}
	while (result % 5)
		result--;
	printf("%d\n", result);
	return 0;
}