Cod sursa(job #1768533)

Utilizator Joystick6208Catalin Topala Joystick6208 Data 1 octombrie 2016 01:43:30
Problema Factorial Scor 5
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.5 kb
#include <cstdio>
using namespace std;

int zero(int n)
{
	int num = 0, c = 5;

	while(c <= n)
	{
		num += n / c;
		c *= 5;
	}

	return num;
}

int main()
{

	int p, st = 1, dr = 2147483646, n, m;

	freopen("fact.in", "r", stdin);
	freopen("fact.out", "w", stdout);

	while(st <= dr)
	{
		m = ((st + dr) >> 1);

		if(zero(m) >= p)
		{
			dr = m - 1;
			n = m;
		}
		else
			st = m + 1;
	}

	if(zero(n) == p)
		printf("%d\n", n);
	else
		printf("-1\n");

	return 0;
}