Cod sursa(job #2177407)

Utilizator EclipseTepes Alexandru Eclipse Data 18 martie 2018 16:11:29
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <iostream>
#include <fstream>
#include <climits>

using namespace std;

typedef long long int lint;

lint n, m, t, p5, s, result;

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

void BinarySearch(lint a, lint b) {
    if (a > b) return;
    else {
        s = 0;
        m = a + (b - a) / 2;
        t = m;
        p5 = 5;
        while (t / p5 > 0) {
            s = s + (t / p5);
            p5 *= 5;
        }
        if (s == n) {
             result = m;
             BinarySearch(a, m - 1);
        }
        else {
            if (s > n) {
                BinarySearch(a, m - 1);
            } else {
                BinarySearch(m + 1, b);
            }
        }
    }
}

int main()
{
    fin >> n;
    if (n == 0) {
        fout << 1;
        return 0;
    }
    BinarySearch(0, INT_MAX);
    if (result == 0) fout << -1;
    else fout << result;
    return 0;
}