Cod sursa(job #2667339)

Utilizator RaresPoinaruPoinaru-Rares-Aurel RaresPoinaru Data 3 noiembrie 2020 12:42:33
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 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;
    if (p==0)
    {
        fout <<1;
    }
    else
    {
        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;
}