专栏文章

题解:P2436 钦定

P2436题解参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@miqestsm
此快照首次捕获于
2025/12/04 03:37
3 个月前
此快照最后确认于
2025/12/04 03:37
3 个月前
查看原文

题解:P2436 钦定

纯暴力就能过的水题。

1. 解题思路

一眼望过去,不就是取模的运算吗?我们可以枚举循环的周期 kk,然后去计算神犇中对 kk 取模中最大的,计算蒟蒻中对 kk 取模最小的。这里为了方便,我们把取模的余数设定为 11kk
令神犇中对 kk 取模中最大的为 pp,蒟蒻中对 kk 取模最小的为 qq,则需要判断 ppqq 的关系是否符合条件,及判断 pp 是否小于 qq。如果搜到了一个符合要求的 ppqq,我们为了满足循环的周期为 kk,再把 qq 设置成 kpk-p。最后我们再枚举循环周期时发现一个可以的 ppqq 就与一个 pair<int, int> 类型的答案去最小值即可。
可以用 pair<int, int> 是因为这个类型重载的小于号是先比较 first,再比较 second,刚好符合题目的要求。

2. 代码实现

就知道你们想要这个,专门删掉了注释。
CPP
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int maxn = 1010;
constexpr int inf = 0x7fffffff;
int n, m, a[maxn], b[maxn];
signed main(signed argc, char* argv[]) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    while (cin >> n >> m) {
    	pair<int, int> ans = {inf, inf};
    	for (int i = 1; i <= n; i++) cin >> a[i];
    	for (int i = 1; i <= m; i++) cin >> b[i];
    	int x = inf, y = inf;
    	bool flg = false;
    	for (int i = 1; i <= (n + m) * 10; i++) {
    		int xx = -inf, yy = inf;
    		for (int j = 1; j <= n; j++) xx = max(xx, a[j] % i ? a[j] % i : i);
    		for (int j = 1; j <= m; j++) yy = min(yy, b[j] % i ? b[j] % i : i);
    		if (xx >= yy) continue;
    		flg = true;
    		yy = i - xx;
    		ans = min(ans, {xx, yy});
		}
		if (!flg) cout << "NO\n";
		else cout << ans.first << " " << ans.second << "\n";
	}
    return 0;
}
在题解的最后,提前祝大家新年快乐!

评论

0 条评论,欢迎与作者交流。

正在加载评论...