Cod sursa(job #2760380)

Utilizator mihnea03Ciocioiu Mihnea mihnea03 Data 25 iunie 2021 19:29:30
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.63 kb
#include <fstream>

using namespace std;
int n,st,dr;

int main() {
    ifstream fin("fact.in");
    ofstream fout("fact.out");
    fin>>n;
    st=1;
    dr=n*5;
    while (st<=dr) {
        int mid=(st+dr)/2;
        int nr=mid;
        int sol=0;
        int m=5;
        while (nr>=m) {
            sol+=nr/m;
            m*=5;
        }
        if (sol>=n) {
            dr=mid-1;
        }
        else {
            st=mid+1;
        }
    }
    int nr=st;
    int sol=0;
    int m=5;
    while (nr>=m) {
        sol+=nr/m;
        m*=5;
    }
    if (sol==n) fout<<st;
    else fout<<-1;
    return 0;
}