chenchen 题解分享 · 2025/3/18
求阶乘(编程题) - 题解
``` #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 6
yuri01 题解分享 · 2025/1/24
求阶乘(编程题) - 题解
```cpp #include <bits/stdc++.h> using namespace std; typedef long long ll; ll k; ll countFives(ll n) { ll result = 0; while (n) { result += n / 5; n /= 5; } return result; } ll binarySearch(ll k) { ll l = 1, r = 5 * k, ans = -1; while (l <= r) { ll m = l + (r - l) / 2; ll numZeros = countFives(m); if (numZeros == k) { ans = m; r = m - 1; } else if (numZeros < k) l = m + 1; else r = m - 1; } return ans; } int main() { cin >> k; cout << binarySearch(k) << endl; return 0; } ```
查看全文
0 0 0 5