Cod sursa(job #2832238)

Utilizator dobreraduDobre Radu Fabian dobreradu Data 13 ianuarie 2022 11:00:19
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <fstream>
#define MAXN 500000000
using namespace std;

ifstream fin( "fact.in" );
ofstream fout( "fact.out" );

int p5( int n ){
  int e = 0, p = 5;
  while( p < n )
    e += ( n / p ), p *= 5;
  return e;
}

int main(){
  int p, st, dr, gasit = 0, mij;
  fin >> p;
  if( p == 0 )
    fout << "1", gasit = 1;
  st = 1, dr = MAXN + 1;
  while( dr - st > 1 && !gasit ){
    mij = (st + dr) / 2;
    if( p5(mij) == p )
      fout << (mij / 5) * 5, gasit = 1;
    else{
      if( p5(mij) < p )
        st = mij + 1;
      else
        dr = mij - 1;
    }
  }
  if( gasit == 0 )
    fout << -1;
  return 0;
}