Cod sursa(job #2063577)

Utilizator ioanailincaMoldovan Ioana Ilinca ioanailinca Data 11 noiembrie 2017 12:11:02
Problema Factorial Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <fstream>

using namespace std;

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

int p, step = 1;

inline int test(int x) {
    int ans = 0;

    for (int i = 5; i <= x; i *= 5)
        ans += x / i;

    return ans;
}

int main()
{
    fin >> p;
    if (p == 0) {
        fout << 1;
        return 0;
    }
    int lim = 0x3f3f3f3f;

    while ((step << 1) <= lim)
        step <<= 1;

    int ans = lim;
    for (; step; step >>= 1) {
        if (ans - step >= 0 && test(ans - step) >= p)
            ans -= step;
    }

    if (test(ans) != p) {
        fout << -1;
    }
    else
        fout << ans;

    fin.close();
    fout.close();
    return 0;
}