Cod sursa(job #2237593)

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

#include <cassert>

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;
}

int64_t caut_bin (int64_t n) {
    int64_t l = 1, h = std::numeric_limits<int64_t>::max(), m = 0;
    
    while (l <= h) {
        m = (l + h) >> 1;
        (numara_zerouri (m) < n) ? l = m + 1 : h = m - 1;
    }
    
    std::vector<int64_t> rez;
    while (numara_zerouri (l) == n) {
        rez.push_back (l); l++;
    }
    
    return ((!rez.empty ()) ? rez.front () : (-1));
}

int main (void) {
    int64_t p;
    assert (fi >> p);
    assert (fo << caut_bin (p));
    return 0;
}