Cod sursa(job #2772418)

Utilizator Gabryel9898Bizdoc Vasile Gabriel Gabryel9898 Data 1 septembrie 2021 03:47:59
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <iostream>
#include <chrono>
#include <fstream>
#include <climits>

using namespace std::chrono;

std::ifstream f1("fact.in");
std::ofstream f2("fact.out");


long long zeros(long long n) {
    long long res = 0;
    while ((n = n / 5)) {
        res += n;
    }
    return res;
}

void factorial() {
    int target;
//    std::ifstream f1("../in.txt");
//    std::ofstream f2("../out.txt");

    f1 >> target;

    long long start = 1;
    long long end = LLONG_MAX - start;
    long long number_of_zeroes;
    long long half;

    do {
        half = (start + end) / 2;
        number_of_zeroes = zeros(half);
        if (number_of_zeroes >= target) {
            end = half;
        } else if (number_of_zeroes < target) {
            start = half + 1;
        }
    } while (start != end);

    if (zeros(start) == target) {
        f2 << start;
    } else {
        f2 << -1;
    }
}


int main() {
    auto start = high_resolution_clock::now();

    factorial();

    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    std::cout << std::endl << "[time]:" << duration.count() << std::endl;
    return 0;
}