题解 | #游游的数值距离#
游游的数值距离
https://www.nowcoder.com/practice/a3688c3615d144e4ba79357034850e3c
#include "bits/stdc++.h" using namespace std; #define int long long #define pb push_back #define endl "\n" #define x first #define y second #define PII pair<int,int> #define PIII pair<int,PII> const int MOD = 1e9 + 7; const int N = 3e5; const int X[] = {1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600, 6227020800, 87178291200, 1307674368000}; int res(int x, int y, int n) { return abs(y * (X[x - 1] - 1) - n); } void slu() { int y = 1; int n; cin >> n; int R = INT_MAX; PII cnt = {1, 1}; R = res(1, 1, n); for (int i = 3; i < 14; i++) { int t = X[i - 1] - 1; if (t < n) { y = n / t; if (y != 2 && R > res(i, y, n)) { cnt = {i, y}; R = res(i, y, n); } y++; if (y != 2 && R > res(i, y, n)) { cnt = {i, y}; R = res(i, y, n); } } else { if (R > res(i, y, n)) { cnt = {i, 1}; R = res(i, y, n); } break; } } cout << cnt.first << " " << cnt.second << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; // cin >> T; T = 1; while (T--)slu(); }