Cod sursa(job #2667334)

Utilizator RaresPoinaruPoinaru-Rares-Aurel RaresPoinaru Data 3 noiembrie 2020 12:37:02
Problema Factorial Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
using namespace std;
ifstream fin ("fact.in");
ofstream fout ("fact.out");
int main()
{
    int p,s,d,m,fact,km,k,ok;
    fin >>p;
    s=1;
    d=p*5;
    ok=0;
    while (s<=d)
    {
        m=(s+d)/2;
        k=5;
        fact=0;
        while (k<=m)
        {
            fact+=m/k;
            k=k*5;
        }
        if (fact<p)
        {
            s=m+1;
        }
        else
        {
            if (fact>p)
            {
                d=m-1;
            }
            else
            {
                m=m-m%5;
                fout <<m;
                s=d+1;
                ok=1;
            }
        }
    }
    if (ok==0)
        fout <<-1;
    fin.close ();
    fout.close ();
    return 0;
}