Cod sursa(job #163742)

Utilizator mgntMarius B mgnt Data 23 martie 2008 00:33:27
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
# include <iostream>
# include <fstream>
using namespace std ;

long long int pow5 ( long long int n ) {
  long long int s = 0 ;
  while ( 0 < n ) {
    n = n / 5 ;
    s += n ;
  }
  return s ;
}

int
main ( ) {
  ifstream sin ( "fact.in" ) ;
  ofstream sout ( "fact.out" ) ;
  long long int a = 1 ;
  long long int b = 0x7fffffffffffffffLL / 2LL;
  long long int c ;
  long long int p ; 
  sin >> p ;
  bool found = false ;
  while ( a <= b ) {
    c = ( a + b ) / 2 ;
    long long int d = pow5 ( c ) ;
    if ( p < d ) {
      b = -1 + c ;
    } else if ( p > d ) {
      a = + 1 + c ;
    } else {
      found = true ;
      a = 1 + b ;
    }
  }
  if ( found ) {
    a = c - ( c % 5 ) ;
    if ( 0 == a ) {
      a = 1 ;
    }
  } else {
    a = - 1 ;
  }
  sout << a << endl ;
  return 0 ;
}