Cod sursa(job #1749754)

Utilizator jurjstyleJurj Andrei jurjstyle Data 28 august 2016 18:06:35
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.95 kb
#include <fstream>
#include <iostream>

using namespace std ;

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

long long N , st , dr , gasit = -1 ;

/*Pt un nr prim p , puterea la care apare in n! este
[n/p] + [n/p^2] + [n/p^3] + ...
in cazul nostru p = 5
*/
long long putere_5 ( long long n ) //returneaza la ce putere apare 5 in descompunerea lui 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 ;
}