Cod sursa(job #2954813)

Utilizator Pop_EmilPal Tamas Pop_Emil Data 15 decembrie 2022 14:57:08
Problema Factorial Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <iostream>
using namespace std;

FILE *in = fopen("fact.in", "r"), *out = fopen("fact.out", "w");
int P;

int nr_of_zeros (int n){
    int res = 0, div = 5;
    while (div <= n){
        res += n / div;
        div *= 5;
    }
    return res;
}

int BS (int L, int R, int P){
    int M, val;
    while (L <= R){
        M = (L + R) / 2;
        val = nr_of_zeros(M);
        if (P < val)
            R = M - 1;
        else if (P > val)
            L = M + 1;
        else {
            while (nr_of_zeros(M - 1) == val)
                --M;
            return M;
        }

    }
    return -1;
}

int main()
{
    fscanf(in, "%d", &P);

    int N;
    if (P == 0)
        N = 1;
    else
        N = BS(1, 100000000, P);

    fprintf(out, "%d", N);
    return 0;
}