Cod sursa(job #2516033)

Utilizator david_octavianoctavian david_octavian Data 30 decembrie 2019 11:17:06
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.06 kb
//#include <iostream>
#include <fstream>
using namespace std;
ifstream cin("fact.in");
ofstream cout("fact.out");
long nrzero(int x)
{
    long p = 0;
     while( x )
     {
            p += x/5;
            x /= 5;
     }
     return p;
}

long cautare_binara(int y)
{
    long long Sol = -1, Left = 0, Right = 1000000000;
    while(Left <= Right)
    {
        int Mid = (Left+Right) / 2;
        if(nrzero(Mid) == y)
        {
            Sol = Mid;
            break;
        }
        if(nrzero(Mid) > y)
            Right = Mid - 1;
        if(nrzero(Mid) < y)
            Left = Mid + 1;
        if(nrzero(Mid) == y)
            return Mid;
    }
    return Sol;
}

int main()
{
    int N=1,P,i,nrzero=1;
    cin>>P;
    if(P==0)
    {
        cout<<1;
        return 0;
    }
    i=cautare_binara(P);
    //cout<<i<<"\n";
   if(i==-1)
    {
        cout<<-1;
        return 0;
    }
    for(int j=i;j>=1;j--)
    {
        if(j%5==0)
        {
            cout<<j;
            return 0;
        }
    }
    return 0;
}