Cod sursa(job #834520)

Utilizator Theodor1000Cristea Theodor Stefan Theodor1000 Data 14 decembrie 2012 16:24:57
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.56 kb
#include <cstdio>

using namespace std;

int p, s, ci, i, x, cx, n, cp;
bool OK;

int main ()
{
	freopen ("fact.in", "r", stdin);
	freopen ("fact.out", "w", stdout);
	
	scanf ("%d", &p);
	cp = 5 * p + 1;
	n = 1;
	
	while (cp >= n)
	{
		x = (cp + n) / 2;
 		s = 0;
		
		for (cx = x; cx > 0;)
		{
			cx /= 5;
			s += cx;
		}
		
		if (s == p) 
			{
				OK = 1;
				break;
		}
		else if (s < p) n = x + 1;
		else cp = x - 1;
	}
	
	if (p == 0) printf ("1\n");
	else if (OK) printf ("%d\n", x / 5 * 5);
	else printf ("-1\n");
	
	return 0;
}