Cod sursa(job #2783504)

Utilizator PopMatei77Matei Pop PopMatei77 Data 14 octombrie 2021 16:27:20
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

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

int nz(long long n) {
    int p = 5;
    int s = 0;
    while (p <= n) {
        s += n / p;
        p *= 5;
    }
    return s;
}

int main() {
    int c = 5;
    int p;
    bool ok = 0;
    fin >> p;
    if (p == 0)
        fout << 1;
    else {
        int st = 1;
        int dr = 2000000000;
        int mj;
        while (st <= dr && ok == 0) {
            mj = (st + dr) / 2;
            if (nz(mj) == p)
            {
                fout << mj-mj%5;
                ok = 1;
            }
            else
                if (nz(mj) < p)
                    st = mj + 1;
                else
                    dr = mj - 1;
        }
        if (ok == 0)
            fout << -1;
    }
}