Cod sursa(job #2735016)

Utilizator Codrin91Codrin Muntean Codrin91 Data 1 aprilie 2021 18:51:00
Problema Factorial Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <iostream>
#include <fstream>

using namespace std;


int nrZerouriFact(int p)
{
    int nr = 0, pow = 5;
    while(pow <= p)
    {
        for(int i = pow; i <= p; i+=pow)
        {
            nr++;
        }

        pow*=5;
    }
    return nr;
}


int main()
{
    ifstream fin("fact.in");
    ofstream fout("fact.out");

    int p, n, a = 0, b, mij;
    fin>>p;
    b = p * 5;

    bool gasit = false;
    while(!gasit && a <= b)
    {

        mij = (a + b) / 2;
        int nrZeroMij = nrZerouriFact(mij);
        if(nrZeroMij == p)
        {
            gasit = true;
            n = mij;
        }
        else if(nrZeroMij > p)
        {
            b = mij - 1;
        }
        else
        {
            a = mij + 1;
        }
    }

    fout<<(gasit?(n/5)*5:-1);

    return 0;
}