Cod sursa(job #2788811)

Utilizator LucasSecaraSecara Lucas Victor LucasSecara Data 26 octombrie 2021 14:51:57
Problema Factorial Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("fact.in");
ofstream fout ("fact.out");
int nz(int n)
{
    int s=0;
    int p=5;
    while (p<n)
    {
        s=s+n/p;
        p*=5;
    }
    return s;
}
int main()
{
    int n, p;
    fin >> p;
    int ok=0, poz=-1;
    int st=1, dr=1000000000;
    if (p==0)
    {
        fout << 1;
    }
    else
    {
        while (st<=dr && ok==0)
        {
            int mij=(st+dr)/2;
            if (nz(mij)==p)
            {
                ok=1;
                poz=mij;
            }
            else if(nz(mij)<p)
                st=mij+1;
            else
                dr=mij-1;
        }
        if(ok==0)
        {
            fout << -1;
        }
        else
            fout << poz-poz%5;
    }
    return 0;
}