Cod sursa(job #1483964)

Utilizator tudorgalatanRoman Tudor tudorgalatan Data 10 septembrie 2015 11:04:04
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <fstream>

using namespace std;

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

int n, p, mij;

int zero (int h)
{
    int e = 0;
    int k = 5;
    while (h >= k)
    {
        e += h/k;
        k *= 5;
    }
    return e;
}

int main ()
{
    int aux;
    fin >> p;
    if (p == 0)
        fout << 1;
    else if (p <= 4)
        fout << 5*p;
    else
    {
        int st = 2*p/5-1;
        int dr = p+1;
        int ok = 1;
        while (dr-st>1 && ok)
        {
            mij = (st+dr)/2;
            aux = zero (5*mij);
            if (aux == p)
                ok = 0;
            else if (aux < p)
                st = mij;
            else if (aux > p)
                dr = mij;
        }
        if (ok)
            fout << -1;
        else
            fout << 5*mij;
    }
    return 0;
}