Cod sursa(job #2923563)

Utilizator Alex18maiAlex Enache Alex18mai Data 15 septembrie 2022 20:07:45
Problema Factorial Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.77 kb
#include <fstream>

using namespace std;

//ifstream cin ("input"); ofstream cout ("output");
ifstream cin ("fact.in"); ofstream cout ("fact.out");

long long zerof(int n){
    long long nr=0;

    while (n){
        nr += n / 5;
        n = n / 5;
    }

    return nr;
}

int main() {

    int p;
    cin>>p;

    int st = 0;
    int dr = 1e9;

    int ans = -1;

    while (st <= dr){
        int mij = (st + dr) / 2;
        if (zerof(mij) <= p){
            st = mij + 1;
            ans = mij;
        }
        else{
            dr = mij - 1;
        }
    }

    if (ans == -1 || zerof(ans) != p){
        cout<<-1<<'\n';
    }
    else{
        //VARIANTA 1
        ans -= ans % 5;
        if (ans == 0){
            ans = 1;
        }

        cout<<ans<<'\n';
    }

    return 0;
}