Cod sursa(job #2429507)

Utilizator MihaIonescuMihai Ionescu MihaIonescu Data 9 iunie 2019 20:41:40
Problema Factorial Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <fstream>
using namespace std;

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

int cntZeros (int n)
{
    int pow = 5;
    int cnt = 0;
    while (pow <= n)
    {
        cnt += (n/pow);
        pow *= 5;
    }
    return cnt;
}

int main ()
{
    int n;
    f>>n;

    int st, dr, mij;
    st = 1;
    dr = cntZeros(100000000);

    bool gasit = false;

    while (st < dr)
    {
        mij = ((st + dr) / 2);
        int cnt = cntZeros (mij);

        if (cnt == n)
        {
            gasit = true;
            break;
        }

        else if (cnt < n)
            st = mij+1;
        else
            dr = mij-1;
    }


    if (gasit)
        g<<(mij/5)*5;
    else
        g<<-1;

    return 0;
}