Cod sursa(job #326539)

Utilizator TabaraTabara Mihai Tabara Data 25 iunie 2009 15:18:52
Problema Factorial Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define in "fact.in"
#define out "fact.out"
#define NMAX (1<<25)

long int nr( long int x )
{
	long int nrsol=0;
	long int i,cpy;
	for ( i = 1; i <= x; ++i )
	{
		cpy = i;
		while ( cpy % 5 == 0 ) cpy /= 5, nrsol++;
	}
	return nrsol;
}

long int bl, bm, br;
long int S, P, last;

int main ( void )
{
	freopen ( in, "r", stdin );
	freopen ( out, "w", stdout );

	scanf ( "%ld", &P );
	
	last = -1;
	for ( bl = 1, br = NMAX; bl <= br; )
	{
		bm = bl + ((br-bl)>>1);
		S = nr(bm);
		if ( S == P ) { last = bm; br = bm-1; }
		else if ( S < P ) bl = bm+1;
		else br = bm-1;
	}
	printf ( "%ld\n", last == -1 ? -1 : last );
	return 0;
}