Cod sursa(job #2569334)

Utilizator lazarandrei13Lazar Andrei lazarandrei13 Data 4 martie 2020 11:51:29
Problema Factorial Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;

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

#define in cin
#define out cout

#define MAX 20000000

int nr0(int p) {
    int sol = 0;
    int aux = 5;

    while (aux <= p) {
        sol += p / aux;
        aux *= 5;
    }
    return sol;
}

int solve(int p) {
    if (p == 0) {
        return 1;
    }

    int st = 1, dr = MAX;
    while (st <= dr) {
        int mij = st + (dr - st) / 2;

        int n0 = nr0(mij);
        if (n0 == p) {
            return mij - mij % 5;
        } else if (n0 > p) {
            dr = mij - 1;
        } else {
            st = mij + 1;
        }
    }
}

int main()
{
    int p;
    in >> p;
    out << solve(p);
    return 0;
}