Cod sursa(job #2793016)

Utilizator vzzev edmond vzze Data 2 noiembrie 2021 18:09:15
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <fstream>

bool check(size_t n, size_t p) {

    size_t x = 0;

    size_t i = 5;

    while(n >= i) {
        x += n / i;
        i *= 5;
    }

    return x >= p;
}

bool _check(size_t n, size_t p) {

    size_t x = 0;

    size_t i = 5;

    while(n >= i) {
        x += n / i;
        i *= 5;
    }

    return x == p;
}

ssize_t find(size_t n) {

    if(n == 0) return 1;

    size_t low = 0;
    size_t high = 5 * n;
    size_t mid;

    while(low <= high) {
        mid = (low + high) / 2;

        if(check(mid, n)) {
            high = mid - 1;
        } else {
            low = mid + 1;
        }
            
    }

    return (_check(low, n)) ? low : -1;
}

int main() {
    std::ifstream f("fact.in");
    std::ofstream o("fact.out");

    size_t n; 
    
    f >> n;

    o << find(n);

    f.close();
    o.close();
}