Cod sursa(job #2676963)

Utilizator yzarayarina yzara Data 25 noiembrie 2020 15:53:20
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <fstream>

using namespace std;

ifstream in("fact.in");
ofstream out("fact.out");

int nr_zero(int n)
{
    int pw = 5;
    int nr_zero = 0;
    while( pw <= n)
    {
        nr_zero += n/pw;
        pw*=5;
    }
    return nr_zero;
}

int p,sol,mj;
long long st=0, dr=1000000005;

int main()
{
    in>>p;
    if ( p==0)
        out<<1;
    else
    {
        while ( st<=dr)
        {
            mj = (st + dr) / 2;
            if ( nr_zero(mj) < p)
                st = mj+1;
            else if (nr_zero(mj) > p)
                dr = mj-1;
            else if (nr_zero(mj) == p)
            {
                sol = mj;
                dr = mj-1;
            }
        }
        if ( sol==0)
            out<<-1;
        else
            out<<sol;
    }
    return 0;
}