Cod sursa(job #2735001)

Utilizator Codrin91Codrin Muntean Codrin91 Data 1 aprilie 2021 18:36:50
Problema Factorial Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;




long long nrZerouriFact(long long p)
{
    long long nr = 0;
    for(long long i = 1; i <= p/5; i+=1)
    {
        long long n = 0, j = i;
        while(j%5==0)
        {
            j/=5;
            n++;
        }
        nr+=n;
    }
    return nr+p/5;
}


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

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

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

        mij = (a + b) / 2;
        long long 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;
}