Cod sursa(job #2291717)

Utilizator sabinpocrisSabin P sabinpocris Data 28 noiembrie 2018 15:41:35
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <bits/stdc++.h>
using namespace std;

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

bool number_of_zeros(int nr, int n, int option){
    int rez = 0;
    for(int i = 5; i <= nr ; i *= 5){
        rez += nr / i;
    }
    
    switch (option){
        case 1:
        return (rez >= n);
        break;
        case 0:
        return (rez == n);
    }
}

int main(){
    int n, fact;

    in >> n;

    if (n == 0){
        out << 1 << "\n";
        return 0;
    }

    if (n == 1){
        out << "5\n";
        return 0;
    }

    int low = 0;
    int high = 5 * n;

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

        if (number_of_zeros(mid, n, 1))
            high = mid;
        else 
            low = mid + 1;
    }
    
    if (number_of_zeros(low, n, 0))
        out << low << endl;
    else 
        out << "-1\n";

    return 0;
}