Cod sursa(job #2622771)

Utilizator StefansenNegulescu Stefan Stefansen Data 1 iunie 2020 19:45:32
Problema Factorial Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include<iostream>
#include<algorithm>
#include<fstream>

using namespace std;

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

long long P;

int nr_fact(long long x)
{
   int nr = 0, p = 1, temp = 5;

   while(temp <= x)
   {
       nr += x / temp;
       p++;
       temp = pow(5,p);
   }

   return nr;
}

long long Cautare(int l,int r)
{
    if(P == 0)
        return 1;
    if(l == r)
        if(nr_fact(l) == P)
            return l;
        else
            return -1;
    else
    {
        long long mid;
        mid = l + (r - l)/ 2;
        if(nr_fact(mid) < P)
            return Cautare(mid + 1,r);
        else
            return Cautare(l,mid - 1);
    }

}



int main()
{
    f>>P;

    g<<Cautare(5,P*10);

    f.close();
    g.close();
    return 0;

}