Cod sursa(job #1389981)

Utilizator raducostacheRadu Costache raducostache Data 16 martie 2015 19:17:38
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>

using namespace std;

ifstream f ("fact.in");
ofstream g ("fact.out");

int p, sol = -1, st = 1, dr = 400000020;

int nr_0 (int x)
{
    if (!x)
        return 1;

    int nr = 0, pas = 5;

    while (pas <= x)
    {
        nr += x / pas;
        pas *= 5;
    }

    return nr;
}

int main()
{
    f >> p;

    int k, mij;

    while (st <= dr)
    {
        mij = (st + dr) / 2;
        k = nr_0 (mij);

        if (k == p)
        {
            sol = mij;

            while (nr_0 (sol) == p)
                -- sol;
            ++ sol;
            break;
        }

        else if (k < p)
            st = mij + 1;

        else
            dr = mij - 1;
    }

    g << sol;

    f.close ();
    g.close ();
    return 0;
}