Cod sursa(job #2483402)

Utilizator eduardcadarCadar Eduard eduardcadar Data 29 octombrie 2019 18:45:53
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <fstream>
#include <climits>
#include <algorithm>
using namespace std;
ifstream f("fact.in");
ofstream g("fact.out");
int p, st(1), dr(INT_MAX), m;
int zero(int i) {
    int nr = 0, x = 5;
    while (x <= i) {
        nr += i/x;
        x *= 5;
    }
    return nr;
}
int main()
{
    f >> p;
    if (!p) {
        g << "1\n";
        return 0;
    }
    while (st <= dr) {
        m = st + (dr - st) / 2;
        if (zero(m) == p) {
            g << m - m%5 << endl;
            exit(0);
        }
        if (zero(m) < p) st = m + 1;
        else dr = m - 1;
    }
    g << "-1\n";
    return 0;
}