Cod sursa(job #2229380)

Utilizator ce_pyCeppy Ceppilescu ce_py Data 6 august 2018 17:32:09
Problema Factorial Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <iostream>

#include <fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int main()
{
    int p;
    fin>>p;
    long long ls=1, ld=LONG_MAX;
    long long x, y, k;
    while(ls<=ld)
    {
        x=(ls+ld)/2;
        while(x%5!=0&&x>1)
        {
            x--;
        }
        k=0;
        long long j, five;

        for(int i=1; i<=INT_MAX; i++)
        {
            j=i; five=1;
            while(j)
            {
                    five*=5;
                    j--;
            }
            if(x>=five)
            {
                k+=x/five;
            }
            else break;
        }

        if(k==p)
        {
            fout<<x;
            return 0;
        }
        else
            if(k>p)
            {
                ld=x-5;
            }
            else ls=x+5;
    }
    fout<<-1;
}