Cod sursa(job #2100379)

Utilizator xtreme77Patrick Sava xtreme77 Data 5 ianuarie 2018 16:06:46
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <fstream>
#include <cmath>

using namespace std;

long long zeros (int number) {
    long long put = 5LL;
    long long sum = 0;
    while (put <= number) {
        sum += number / put;
        put *= 5LL;
    }
    return sum;
}

int main() {
    ifstream input ("fact.in");
    ofstream output ("fact.out");

    int P;
    input >> P;
    int solution = -1;
    int lim = 10 * P;
    int sqr = (int)sqrt(double(lim / 2));
    for (int currentNumber = 1 ; currentNumber <= lim ; currentNumber += sqr) {
        if (zeros(currentNumber + sqr - 1) >= P) {
            for (int number = currentNumber ; number <= currentNumber + sqr - 1 ; ++ number) {
                if (zeros(number) == P) {
                    solution = number;
                    break;
                }
            }
            break;
        }
    }
    if (zeros(solution) != P) {
        output << -1 << '\n';
        return 0;
    }
    solution = solution - solution % 5;
    if (solution == 0) {
        output << 1 << '\n';
    }
    else {
        output << solution << '\n';
    }
    return 0;
}