Cod sursa(job #2256697)

Utilizator IordachescuAncaFMI Iordachescu Anca Mihaela IordachescuAnca Data 8 octombrie 2018 23:21:13
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.74 kb
#include<fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");
int n,s,d,ok,mij,x,c,sol,i,nr;
int main()
{
    fin>>n;
    if(n==0)
    {
        fout<<1;
        return 0;
    }
    ok=0;
    s=0;
    d=5*n;
    while(s<=d)
    {
        mij=(s+d)/2;
        nr=5;
        c=0;
        while(mij>=nr)
        {
            c=c+mij/nr;
            nr=nr*5;
        }
        if(c==n)
        {
            fout<<mij-mij%5;
            ok=1;
            break;
        }
        if(c<n)
        {
            s=mij+1;
        }
        if(c>n)
        {
            s=mij-1;
        }
    }
    if(ok==0)
    {
        fout<<-1;
    }
    fin.close();
    fout.close();
    return 0;
}