Cod sursa(job #1267022)

Utilizator MoneaVladMonea Vlad MoneaVlad Data 19 noiembrie 2014 14:09:26
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.66 kb
#include <fstream>

using namespace std;
ifstream in("fact.in");
ofstream out("fact.out");

int factorial(int x){
    int S = 0;
    for(int i = 5; i <= x; i = i * 5)
        S = S + x / i;
    return S;
}

int main()
{
    int st, dr, mid, p;
    in >> p;
    st = 1;
    dr = 1000000000;
    while (dr - st > 1){
        mid = st + (dr - st) / 2;
        if (factorial (mid) >= p)
                    dr = mid;
                    else
                    st = mid;
    }
    if (factorial (st) == p)
        out << st;
        else
        if (factorial (dr) == p)
            out<< dr;
            else
            out << -1;


}