Cod sursa(job #2914518)

Utilizator alin.gabrielAlin Gabriel Arhip alin.gabriel Data 20 iulie 2022 10:00:37
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <fstream>
#include <map>
using namespace std;

long zeroes(long m, long p) {
    long count = 0;
    long f = 5;

    while (f <= m) {
        count += m / f;
        f = f * 5;
    }

    return count;
}

long findN(int p) {

    if (p == 0)
        return 1;

    if (p == 1)
        return 5;

    long l = 0;
    long h = 5 * p;

    while (l < h) {
        long m = (l + h) >> 1;

        if (zeroes(m, p) >= p)
            h = m;
        else 
            l = m + 1;
    }
    
    if (zeroes(l, p) == p)
        return l;

    return -1;
}

int main() {
    ifstream fin("fact.in");
    ofstream fout("fact.out");
    int p;
    fin>>p;
    fout<<findN(p);
    return 0;
}