Cod sursa(job #2785439)

Utilizator mihai002016Zaharia Teodor Mihai mihai002016 Data 18 octombrie 2021 18:03:23
Problema Factorial Scor 75
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.13 kb

#include <fstream>
using namespace std;
ifstream fin("fact.in");
ofstream fout("fact.out");

int main()
{
    int p;
    fin >> p;
    int p5 = 1,p5i=0;
    int x = 5,y,xi;
    while (p5 < p)
    {
        p5i = p5;
        p5 = p5 * 5 + 1;
        x = x * 5;
    }
    if (p == 0)
        fout << 1;
    else
    if (p5 == p)
        fout << x;
    else
    if(p5-p>=p-p5i)
    {
        p5 = (p5-1)/5;
        x = x / 5;
        while (p5 < p)
        {
            x = x + 5;
            y = x;
            while (y % 5 == 0)
            {
                p5++;
                y = y / 5;
            }
        }
            if (p5 > p)
                fout << -1;
            else
                fout << x;
    }
    else
    {
        p5i = p5;
        xi = x;
        while (p5i > p)
        {
            y = xi;
            while (y % 5 == 0)
            {
                p5i--;
                y = y / 5;
            }
            xi = xi - 5;
        }
                if (p5i < p)
                    fout << -1;
                else
                    fout << xi;
    }
}