Cod sursa(job #2772414)

Utilizator Gabryel9898Bizdoc Vasile Gabriel Gabryel9898 Data 1 septembrie 2021 03:04:01
Problema Factorial Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 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 end = LLONG_MAX;
    long long start = 1;
    long long zr;
    long long half;
    do {
        half = (start + end) / 2;
        zr = zeros(half);

        if (zr == target) {
            end = half;
            if (start != end) {
                continue;
            } else {
                f2 << start;
                return;
            }
        }

        if (start == end) {
            f2 << -1;
            return;
        }

        if (zr > target) {
            end = half;
        } else {
            start = half + 1;
        }


    } while (true);
}


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