Cod sursa(job #2986814)

Utilizator eneagoeEugen Neagoe eneagoe Data 1 martie 2023 11:47:46
Problema Factorial Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("fact.in");
ofstream fout("fact.out");

inline unsigned long long count_zeros(unsigned long long n)
{
    unsigned long long fives = 0, current = 5;

    while(n >= current) {
        fives += n / current;
        current *= 5;
    }

    return fives;
}

int main(void)
{
    unsigned long long n, z, result = -1, left = 1, right = 1e18, mid;
    bool found = false;

    fin >> n;

    while(left <= right) {
        mid = (left + right) / 2;

        z = count_zeros(mid);

        if(z == n)
            result = mid, right = mid - 1, found = true;
        else if(z > n)
            right = mid - 1;
        else
            left = mid + 1;
    }

    if(found)
        fout << result - result % 5;
    else
        fout << -1;

    return 0;
}