跳到正文

题解

题解:P18115 [NOIP2026] 和弦 / chord(民间数据)

_rft_约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年11月28日
收录
2026年11月28日
更新
2026年11月28日
篇幅
约 3 分钟
获赞
1
题目
P18115

版本

  1. v1当前

P18115 [NOIP2026] 和弦 / chord Solution

Upd

  • 2026-11-28:初稿。

分析

求 lcm⁡(a+x,b+x)\operatorname{lcm}(a+x,b+x) 的最小值,其中 xx 为自然数。

两边都有 xx 不好做,不妨设 a>ba > b,那么 gcd⁡(a+x,b+x)=gcd⁡(a−b,b+x)\gcd(a+x,b+x) = \gcd(a-b, b+x)。

于是可以枚举 a−ba-b 的约数,对于每个约数求出大于等于 bb 的最小倍数,进而求出 xx。

对于每一个 xx 求出答案,然后取 min⁡\min 即可。初值设为 lcm⁡(a,b)\operatorname{lcm}(a,b) 即可。

代码

Code
C++
#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;
}

这道题目 3030 分钟足够了。

祈祷 CCF 不要卡我 ~

原文