Cod sursa(job #140872)

Utilizator sory1806Sandu Sorina-Gabriela sory1806 Data 22 februarie 2008 13:28:14
Problema Factorial Scor 35
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.91 kb
#include<fstream.h>  
unsigned long n, np;  
unsigned long x[13]={0, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, 48828125, 244140625};  
ifstream f("fact.in");  
ofstream g("fact.out");  
      
int factorial(int n)  
{   unsigned long nr, i;  
    for(i=12; i>0; i--)  
    {   nr+=n/x[i];  
    }  
    return nr;  
}  
   
int binara(unsigned long p, unsigned long u)  
{   unsigned long mj;  
    if(p<=u)  
    {   mj=(p+u)/2;  
        if(factorial(mj)==np)  
            return mj;  
        else  
            if(factorial(mj)>np)  
                 return binara(p, mj-1);  
            else  
                 return binara(mj+1, u);  
    }  
    else  
        return -1;  
}  
   
int main()  
{       f>>np;  
	if(np==0)  
	       g<<1;  
	else  
        {   n=binara(1, 3000000000);  
            g<<n;  
        }  
        g.close();  
        return 0;  
}