题解 | #游游的数值距离#
游游的数值距离
https://www.nowcoder.com/practice/a3688c3615d144e4ba79357034850e3c
枚举所有 x ,可能的 y 最多只有三个,x 也很少因为是阶乘
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 2e5 + 5; int __t = 1, n, y, fac = 2, ans, ansx = 1, ansy = 1; void ck(int x, int y) { if (y == 2 || y == 0) return; if (abs((fac - 1) * y - n) < ans) { ans = abs((fac - 1) * y - n); ansx = x; ansy = y; } return; } void solve() { cin >> n; ans = n; for (int x = 3; fac <= n; x++) { fac *= x; y = n / (fac - 1); ck(x, y - 1); ck(x, y); ck(x, y + 1); } cout << ansx << ' ' << ansy << endl; return; } int32_t main() { #ifdef ONLINE_JUDGE ios::sync_with_stdio(false); cin.tie(0); #endif // cin >> __t; while (__t--) solve(); return 0; }