Cod sursa(job #3232302)

Utilizator Gergo123Schradi Gergo Gergo123 Data 29 mai 2024 21:16:15
Problema Factorial Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int f2(int N) {
    int c= 0;
    for (int i = 5; N / i >= 1; i *= 5) {
        c= N / i;
    }
    return c;
}

int f(int P) {
    if (P == 0) {
        return 1;
    }
    int low = 0, high = 5 * P;
    while (low < high) {
        int mid = low + (high - low) / 2;
        if (f2(mid) < P) {
            low = mid + 1;
        } else {
            high = mid;
        }
    }
    return (f2(low) == P) ? low : -1;
}

int main() {
    int P;
    fin>> P;
    int N = f(P);
    fout << N << endl;
    return 0;
}