Cod sursa(job #542133)

Utilizator stef2503Stefan Stan stef2503 Data 25 februarie 2011 20:24:56
Problema Factorial Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 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, st, dr, t;
    scanf("%d", &p);
    st=1;
    dr=1000000;
    while (st<=dr) {
        t=(st+dr)/2;
        if (p==fact(t)) {
            while (p==fact(t)) {
                t--;
            }
            printf ("%d", t+1);
            break;
        } else if (p<fact(t)) {
            dr=t-1;
        } else {
            st=t+1;
        }
    }
    return 0;
}