Cod sursa(job #2504229)

Utilizator cacior_mariusCacior Marius-Valentin cacior_marius Data 4 decembrie 2019 17:45:01
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.51 kb
#include <fstream>

using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int main()
{
	long long n, m=0, p, i;
	fin >> n;
	while(n>=5*m+1)
	{
		m=5*m+1;
	}
	p=m;
	i=1;
	m=0;
	while(n-p!=0)
	{
		if(n-p>0)
		{
			i++;
			n=n-p;
			if(p<=1)
				m+=p;
			else
				m=m+p-(p-1)/5;
		}
		else
		{
			p=(p-1)/5;
			i=1;
		}
	}
	m=m+p-(p-1)/5;
	if(i==5)
	{
		fout<<-1;
	}
	else
	{
		if(m==0)
		{
			fout<<1;
		}
		else
		{
			fout <<5*m;
		}
	}
	return 0;
}