Cod sursa(job #2486373)

Utilizator DragosGavrusDragos Gavrus DragosGavrus Data 2 noiembrie 2019 19:38:28
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f ("fact.in");
ofstream g ("fact.out");

int nrzero(int x) {
    int p = 5, ct = 0;
    while (p <= x) {
        ct += x / p;
        p *= 5;
    }
    return ct;
}

void fin(int x, int p) {
    while (nrzero(x - 1) == p && x > 1)
        x --;
    g << x;
}

void cautare(int p, int st, int dr) {
    while (st <= dr) {
        int mij = (st + dr) / 2;
        int x = nrzero(mij);
        if (x == p) {
            fin(mij, p);
            return ;
        } else if (x > p) {
            dr = mij - 1;
        } else {
            st = mij + 1;
        }
    }
    g << "-1";
}

int main() {
    int p;
    f >> p;
    if (p == 0)
        g << "1";
    else
        cautare(p, 1, 5 * p);
    return 0;
}