Cod sursa(job #1268650)

Utilizator sherban26FMI Mateescu Serban-Corneliu sherban26 Data 21 noiembrie 2014 11:10:37
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.78 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin ("fact.in");
ofstream fout ("fact.out");

long _div(long x)
{
    long d = 5, rez = 0;

    while (x / d)
    {
        rez += x / d;
        d *= 5;
    }

    return rez;
}

int main()
{
    long n, d, f = 5, last, aux = 0;

    fin >> n;

    if (n == 0)
        fout << 1;
    else
    {
        while (true)
        {
            d = _div(f);
            if (d == n)
            {
                fout << f;
                break;
            }

            aux = f + 5 * (n - d);
            if (last == aux)
            {
                fout << -1;
                break;
            }
            last = f;
            f  = aux;
        }
    }

    return 0;
}