Cod sursa(job #1313149)

Utilizator czlateaZlatea Cezar czlatea Data 10 ianuarie 2015 12:46:56
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.53 kb
#include <cstdio>
using namespace std;

int p;

int d(int n)
{
    int x = 0;
    for(; n; x += n / 5, n /= 5);
    return x;
}

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

    scanf("%d", &p);

    int st = 1, dr = 5 * p, m, r = 1;
    while(st <= dr)
    {
        m = (st + dr) / 2;
        if(d(m) >= p)
            r = m, dr = m - 1;
        else
            st = m + 1;
    }

    if(d(r) == p)
        printf("%d\n", r);
    else printf("-1\n");
    return 0;
}