Cod sursa(job #1768584)

Utilizator dinurosca03Rosca Dinu Cristian dinurosca03 Data 1 octombrie 2016 10:52:54
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <fstream>
#include <iostream>

using namespace std ;

ifstream f ("fact.in") ;
ofstream g ("fact.out") ;

long long N , st , dr , gasit = -1 ;
long long putere_5 ( long long n )
{
 long long nr = 0 , p = 5 ;
 while ( n >= p )
    nr += n/p , p*= 5 ;
 return nr ;
}


int main ()
{
 f >> N ;
 st = 1 , dr = 1e18 ;
 while ( st <= dr )
    {
     long long mij = ( st + dr ) / 2 ;
     long long val = putere_5 ( mij ) ;
     if ( val == N )
        {
         if ( putere_5 ( mij - 1 ) != N )
            gasit = mij , st = dr + 1 ;
         else
            dr = mij - 1 ;
        }
     else if ( val > N )
        dr = mij - 1 ;
     else
        st = mij + 1 ;
    }
 if ( N == 0 )
    {
     g << "1" ;
     return 0 ;
    }
 g << gasit ;
}