P18115 [NOIP2026] 和弦 / chord Solution
Upd
- 2026-11-28:初稿。
分析
求 的最小值,其中 为自然数。
两边都有 不好做,不妨设 ,那么 。
于是可以枚举 的约数,对于每个约数求出大于等于 的最小倍数,进而求出 。
对于每一个 求出答案,然后取 即可。初值设为 即可。
代码
Code
#include <bits/stdc++.h>
#define FILE(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define DEBUG(s) freopen(s".err", "w", stderr)
#define CloseFILE fclose(stdin), fclose(stdout)
#define CloseDEBUG fclose(stderr)
#define int long long
#define endl "\n"
using namespace std;
vector<int> ys;
void solve() {
int a, b;
cin >> a >> b;
if (a<b) swap(a,b);
int c = a-b, ans = a/__gcd(a,b)*b;
ys.clear();
ys.push_back(1); ys.push_back(c);
for (int i = 2; i*i <= c; i++) {
if (c%i == 0) {
ys.push_back(i);
if (i*i != c) ys.push_back(c/i);
}
}
for (int d : ys) {
double bd = 1.0*b/d;
int minx = ceil(bd)*d - b;
if (__gcd(c, b+minx) != d) continue;
ans = min(ans, (b+minx)/d*(a+minx));
}
cout << ans << endl;
}
signed main() {
// FILE("chord"), DEBUG("chord");
cin.tie(0) -> sync_with_stdio(0);
int c,t; cin >> c >> t;
while (t--) solve();
// CloseFILE, CloseDEBUG;
return 0;
}这道题目 分钟足够了。
祈祷 CCF 不要卡我 ~