Cod sursa(job #542138)

Utilizator stef2503Stefan Stan stef2503 Data 25 februarie 2011 20:32:27
Problema Factorial Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.76 kb
#include <stdio.h>

int pow5 (int x) {
    int i, t=1;
    for (i=1; i<=x; i++)
        t*=5;
    return t;
}

int fact (int n)
{
    int k=1, c=0;
    while (n/pow5(k)) {
        c+=n/pow5(k);
        k++;
    }
    return c;
}

int main ()
{
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);
    int p, t;
    scanf("%d", &p);
    unsigned int st, dr;
    st=1;
    dr=4000000000;
    while (st<dr) {
        t=(st+dr)/2;
        if (p==fact(t)) {
            while (p==fact(t)) {
                t--;
            }
            printf ("%d", t+1);
            return 0;
        } else if (p<fact(t)) {
            dr=t-1;
        } else {
            st=t+1;
        }
    }
    printf ("-1");
    return 0;
}