Cod sursa(job #50947)

Utilizator tvladTataranu Vlad tvlad Data 9 aprilie 2007 14:21:01
Problema Factorial Scor 85
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.59 kb
#include <cstdio>

int f ( int x ) {
	int s = 0;
	for (int a = x; a > 0; a /= 5) {
		s += a/5;
	}
	return s;
}

int bsearch ( int val )
{
	int step, i;
	const int N = 1e9;
	for (step = 1; step < N; step <<= 1);
	for (i = 0; step; step >>= 1)
		if (i + step < N && f(i + step) <= val)
			i += step;
	return i;
}

int main() {
	freopen("fact.in","r",stdin);
	freopen("fact.out","w",stdout);
	
	int p;
	scanf("%d",&p);
	if (p == 0)
		printf("0");
	else {
		int x = bsearch(p);
		x -= x%5;
		while (f(x-5) == f(x)) x -= 5;
		printf("%d %d %d",x,f(x),f(x-5));
	}
	return 0;
}