Cod sursa(job #3152489)

Utilizator DariusHHanganu Darius DariusH Data 25 septembrie 2023 13:06:37
Problema Factorial Scor 55
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <bits/stdc++.h>

using namespace std;

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

int countzero(int n) {
    int p2, p5, d2, d5;
    d2 = d5 = 0;

    if(n == 0) {
        return 0;
    }

    p2 = 2;
    while(p2 <= n) {
        d2 += n / p2;
        p2 *= 2;
    }

    p5 = 5;
    while(p5 <= n) {
        d5 += n / p5;
        p5 *= 5;
    }

    return min(d2, d5);
 }

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


    if(countzero(st) == p) {
        fout << st + (st == 0) << "\n";
    }else if(countzero(dr) == p) {
        fout << dr + (dr == 0) << "\n";
    }else{
        fout << "-1\n";
    }


    return 0;
}