Cod sursa(job #3174959)

Utilizator fortyforBroscoi Mihai fortyfor Data 25 noiembrie 2023 11:06:18
Problema Factorial Scor 15
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <iostream>
#include <cmath>
#include <fstream>

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

unsigned long long Zero_La_Final_Fact(unsigned int n) {
    n-=n%5;
    unsigned long long s=0,p=5;
    while (p<=n){
        s+=n/p;
        p*=5;
    }
    return s;
}

int main()
{
    unsigned int p;
    fin >> p;
    if (p==0) {
        fout << '1';
        return 0;
    }
    unsigned int r=24999999,l=1,probe=(l+r)/2,ans=r+1;
    while (l<=r) {
        unsigned int zeroes=Zero_La_Final_Fact(probe);
        if (zeroes>=p) {
            if (zeroes == p && ans > probe) {
                ans=probe;
            }
            l=probe+1;
        } else {
            r=probe-1;
        }
        probe=(l+r)/2;
    }
    if (ans==25000000) { fout << "-1"; } else {
    fout << ans;}
    return 0;
}