Cod sursa(job #2048390)

Utilizator theoioanaTheodoraD theoioana Data 25 octombrie 2017 23:21:40
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <cmath>
#include <algorithm>

using namespace std;

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

int n, st, dr, mid, p;

long long fact( long long x ){

         int nr=0, p=5;

         while( p<=x){
                  nr+=x/p;
                  p*=5;
         }
         return nr;
}



int main(){

         fin>>p;
         if( p== 0)
                  fout<<1;
         else{

                  st=0; dr=1000000000;

                  while( st<=dr ){
                           mid=(st+dr)/2;
                           if( fact(mid) >= p )
                                    dr=mid-1;
                           else
                                    st=mid+1;
                  }

                  if ( fact(st)!=p)
                           fout<<-1;
                  else
                           fout<<st;
         }

return 0;
}