Cod sursa(job #593437)

Utilizator qwertyuPeter Eke qwertyu Data 2 iunie 2011 19:46:17
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.69 kb
#define _CRT_SECURE_NO_DEPRECATE

#include <stdio.h>
#include <stdlib.h>


int zero(int x)
{
	int i=0,j=5,q=1;
	
	while (q != 0)
	{
		q = x / j;
		i += q;
		j*=5;
	}

	return i;
}

int main()
{
	FILE * f = fopen("fact.in","r");
	int p,n,q;
	fscanf(f,"%d",&p);
    fclose(f);

	int a[141];
	int b[141];

	for (int i=0; i<141; i++)
	{
		a[i] = 5*i*i*i*i;
		b[i] = zero(a[i]);
	}

	
	int d,c=0;
	n=5*p;
	while ((q=zero(n))>p) 
	{
		d=q-p;
		c=0;
		while ((d>=b[c+1])&&(c<140)) c++;
		n-=a[c];
	}
	

	if (n == 0) n++;

	f = fopen("fact.out","w");
	if (p != q) 
	{
		fprintf(f,"%d",-1);
	}
	else
	{
		fprintf(f,"%d",n);
	}
	fclose(f);
	
	return 0;
}