Cod sursa(job #942113)

Utilizator ShadoWolfCodrut Constantin Gusoi ShadoWolf Data 20 aprilie 2013 20:03:23
Problema Factorial Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <fstream>
using namespace std;
 
long fact(long a,long min=0,long max=100000000);
long number(long x);
int main()
{
    long a;
    ifstream in("fact.in");
    in>>a;
    in.close();
    ofstream out("fact.out");
    out<<fact(a)<<"\n";
    out.close();
    return 0;
}
 
long number(long x)
{
	long pow5=5;
	long c=0;
	while(pow5<=x)
	{
		c+=x/pow5;
		pow5*=5;
	}
	return c;
}
 
long fact(long a,long min,long max)
{  
    if (a==0) return 1;
	if (min>max)
	{
		return -1;
	}
	long mid=(max+min)/2;
	long num=number(mid);
	if(a==num)
	{
		return mid-mid%5;
	}
	else if (a<num)
	{
		return fact(a,min,mid-1);
	}
	else
	{
		return fact(a,mid+1,max);
	}
}