Cod sursa(job #1093625)

Utilizator A63N7pTudor Nazarie A63N7p Data 28 ianuarie 2014 13:29:52
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
using namespace std;

#define NMAX 1000000000

ifstream in;
ofstream out;

int factorial(int x);

int main(int argc, char *argv[])
{
    in.open("fact.in");
    out.open("fact.out");
    int p, val;
    in >> p;
    if (p == 0)
        out << "1" << endl;
    else {
        int min = 1, max = NMAX, mid;
        bool ok = false;
        while (min <= max && !ok) {
            mid = (min + max) / 2;
            val = factorial(mid);
            if (val < p)
                min = mid + 1;
            else if (val > p)
                max = mid - 1;
            else
                ok = true;
        }
        if (ok) {
            while (mid % 5)
                mid--;
            out << mid << endl;
        }
        else
            out << "-1" << endl;
    }
    in.close();
    out.close();
    return 0;
}

int factorial(int x)
{
    int a = 5, rez = 0;
    while (x / a) {
        rez += x / a;
        a *= 5;
    }
    return rez;
}