Cod sursa(job #759184)

Utilizator SchumiDumitru Andrei Georgian Schumi Data 17 iunie 2012 00:05:49
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <cstdio>

using namespace std;

int n, x;

int descompunere(int x)
{
    int c = 0, d = 5;
    while(d <= x) {
        c += x / d;
        d *= 5;
    }
    return c;
}

int cauta(int left, int right)
{
    int mij, sol = -1, verifica;
    while(left <= right) {
        mij = (left + right) / 2;
        verifica = descompunere(mij);
        if(verifica == n) {
            sol = mij;
            right = mij - 1;
        }
        else if(verifica > n)
            right = mij - 1;
        else left = mij + 1;
    }
    return sol;
}



int main()
{
    freopen ("fact.in", "r", stdin);
    freopen ("fact.out", "w", stdout);
    scanf("%d", &n);
    if(n == 0) {
        printf("1");
        return 0;
    }
    x = cauta(1, n * 5);
    if(x != -1)
        printf("%d", x);
    else printf("-1");
    return 0;
}