Cod sursa(job #671944)

Utilizator repp4raduRadu-Andrei Szasz repp4radu Data 1 februarie 2012 10:22:08
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <cstdio>

using namespace std;

int n;

void citire()
{
    freopen("fact.in", "r", stdin);
    scanf("%d", &n);
    fclose(stdin);
}

int nrZero(int a)
{
    int p = 1;
    int s = 0;
    for(int i = 1; i <= 20; i++)
    {
        p *= 5;
        if(p > a)
            break;
        s += (a / p);
    }
    return s;
}

int main()
{
    citire();
    freopen("fact.out", "w", stdout);
    if(n)
    {
        int left = 1;
        int right = 5 * (n + 1);
        int mij;
        int zeros;
        while(left <= right)
        {
            mij = (left + right) >> 1;
            zeros = nrZero(mij);
            if(n > zeros)
            {
                left = mij + 1;
            }
            else if(n < zeros)
            {
                right = mij - 1;
            }
            else
            {
                printf("%d", mij / 5 * 5);
                break;
            }
        }
        if(left > right)
        {
            printf("-1");
        }
    }
    else
    {
        printf("1");
    }
    return 0;
}