Cod sursa(job #734257)

Utilizator mihaitza22Mihai Nan mihaitza22 Data 13 aprilie 2012 21:25:33
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <stdio.h>

long long p;

long long zfact(long long n)
{
    long long s = 0;
    while (n)
    {
        s += n / 5;
        n /= 5;
    }

    return s;
}


int main()
{
    freopen("fact.in", "r", stdin);
    freopen("fact.out", "w", stdout);
    scanf("%lld", &p);
    if ( p == 0 )
    {
        printf("%d\n", 1);
        return 0;
    }
    long long c;
    long long a = 0, b = 10000000000LL;
    long long f;
    while ( a < b-1 )
    {
        c = (a + b)/2;
        f = zfact(c);

        if ( f < p )
            a = c;//+1;
        else if ( f > p )
            b = c;//-1;
        else
            break;
    }

    while ( c % 5 )
        --c;

    if ( zfact(c) == p )
        printf("%lld", c);
    else
        printf("%d", -1);

    return 0;
}