携程T3只过了83.3%,求帮看看是啥问题
携程T3只过了83.3%,求帮看看是啥问题
#include <bits/stdc++.h> using namespace std; inline long long read(){ long long x = 0; char ch = getchar(); while(ch < '0' || ch > '9'){ ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x; } long long n; // |x! * y - y - n| 最小 // 即 构造 x, y, 使得 (x! - 1) * y 最接近 n // info[i] = i! - 1 long long info[] = {-1, 0, -1, 5, 23, 119, 719, 5039, 40319, 362879, 3628799, 39916799, 479001599}; int main(){ n = read(); long long x = 1, y = 1; long long mmin = 0x7FFFFFFFFFFFFFFF; // 枚举 所有的 (x! - 1), 计算 使 (x! - 1) * y 最接近 n 的 y for(long long i = 3; i < 13; i++){ if(info[i] > n){ break; } long long mod = n % info[i]; mod = min(mod, abs((n / info[i]) * info[i] - n)); if(mod < mmin){ mmin = mod; x = i; y = n / info[i]; } } cout << x << ' ' << y << endl; return 0; }