Cod sursa(job #2237615)

Utilizator tangerine515Alex Anton tangerine515 Data 2 septembrie 2018 13:59:13
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <iostream>
#include <fstream>
#include <limits>
#include <vector>

std::fstream fi ("fact.in", std::ios::in);
std::fstream fo ("fact.out", std::ios::out);

int64_t numara_zerouri (int64_t n) {
    int64_t c = 0;
    for (int64_t d = 5; n / d > 0; d = (d << 2) + d) c += n / d;
    return c;
}

int main (void) {
    int64_t p; 
    fi >> p;
    int64_t rez = [&] () -> int64_t {
        int64_t l = 1, h = std::numeric_limits<int64_t>::max(), m = 0;
        while (l <= h) {
            m = (l + h) >> 1;
            (numara_zerouri (m) < p) ? l = m + 1 : h = m - 1; }
        std::vector<int64_t> rez;
        while (numara_zerouri (l) == p) {
            rez.push_back (l); l++;
        }
        return ((!rez.empty ()) ? rez.front () : (-1));
    } ();
    fo << rez;
    return 0;
}