返回题解分享
讨论 / 题解分享/ 帖子详情

求阶乘(编程题) - 题解

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef pair<int,int> aII;  
using ll = long long;
using ULL = unsigned long long;
const int N = 1e6+5;
ll k;
ll check(ll x) {
    ll res = 0;
    for (; x; x /= 5) res += x/5;
    return res;
}
inline void solve() { 
    cin >> k;
    ll l = 0, r = 1e19;
    while (l < r) {
        ll mid = (l + r) >> 1;
        if (check(mid) >= k) r = mid;
        else l = mid + 1;
    }
    if (check(l) == k) cout << l << endl;
    else cout << -1 << endl;
}
int main() { 
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1; 
    //int _; cin >> _;
    while (_--) solve();
    return 0;
}
0 回复 0 转发 0 喜欢 5 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!