Cod sursa(job #2120389)

Utilizator tiberiu392Tiberiu Ungurianu tiberiu392 Data 2 februarie 2018 13:28:58
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <fstream>

using namespace std;

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

int fact(int x)
{
int rez = 0 , p = 5;
  while( p < x )
    {
        rez = rez+x/p;
        p*=5;
    }
    if(!rez)
    return 1;
    return rez;
}

int cautbin(long long n)
{   long long mj = 0;
    long long st = 0;
    long long dr = 1000000000;
    while(st < dr)
      {
          mj = (st+dr)/2;
          if(fact(mj) > n)
          dr = mj;
          else
          st = mj+1;
      }
   return st;
}
int main()
{
     f >> n;
     if(n==0)
      g << 1;
    else
        if(fact(cautbin(n)-5) != n)
            g << -1;
        else
            g << cautbin(n)-5;
    return 0;
}