Cod sursa(job #470146)

Utilizator centaphiBarladeanu Anca centaphi Data 11 iulie 2010 17:15:03
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include<fstream>
#include<math.h>

using namespace std;

long nr;

int min(long a, long b)
{
	if(a<b) return a;
	else
		return b;
}

int nrOf(int n, int i)
{
	int k=0;
	while(n%i==0)
	{
		n=n/i;
		k++;
	}

	return k;
}

int smallest()
{
	bool found=false;
	long nrof2=0;
	long nrof5=0;
	long i=0;

	while(found==false)
	{
		if(nr==0) 
			return 1;
		else if(nr==1)
			return 0;
		else
		{
			for(int j=1;j<=i;j++)
			{
				nrof2+=nrOf(j, 2);

				nrof5+=nrOf(j, 5);
			}			

			if(min(nrof2, nrof5)==nr)
				return i;

			i++;

			nrof2=0;
			nrof5=0;
		}

		if(i>400000)
			return -1;
	}
}

int main()
{	
	ifstream fin;
	ofstream fout;

	fin.open("fact.in");
	fin>>nr;	
	fin.close();	

	fout.open("fact.out");
	
	fout<<smallest();
	
	fout.close();

	return 0;
}