Cod sursa(job #1348629)

Utilizator andreiionutBude Andrei-Ionut andreiionut Data 19 februarie 2015 19:57:05
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.6 kb
#include <cstdio>

using namespace std;

int f0(int n) {
    int ret = 0, p = 5;
    while (n >= p) {
        ret += n / p;
        p *= 5;
    }

    return ret;
}

int main() {
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);

    int N; scanf("%d", &N);

    int poz = 0, pas = 1<<29;
    while (pas >>= 1) {
        if (f0(pas + poz) <= N) {
            poz += pas;
        }
    }
    poz -= 4;

    if (f0(poz) == N) {
        if (poz == 0) poz = 1; // 0! = 1!
        printf("%d", poz);
    } else {
        printf("-1");
    }

    return 0;
}