跳到正文

题解

CF1835A 题解

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

文章信息

发布
2023年12月12日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 3 分钟
题目
CF1835A

版本

  1. v1当前

洛谷传送门 & CF 传送门

思路

题目中有句很重要的话:

Each input file has at most 55 test cases which do not satisfy A,B,C≤3A,B,C\leq3.

这意味着 O(10A)O(10^A) 的做法是可行的。

我们可以枚举每个 aa,枚举范围是 10A∼10A+1−110^A\sim10^{A+1}-1。对于每个 aa 对应的解法就是 max⁡(10B−1,10C−1−i)∼min⁡(10B−1,10C−1−a)\max(10^{B-1},10^{C-1}-i)\sim\min(10^B-1,10^C-1-a),解法数就是 min⁡(10B−1,10C−1−a)−max⁡(10B−1,10C−1−i)+1=min⁡(10B,10C−a)−max⁡(10B−1,10C−1−i)\min(10^B-1,10^C-1-a)-\max(10^{B-1},10^{C-1}-i)+1=\min(10^B,10^C-a)-\max(10^{B-1},10^{C-1}-i) 种。对于每个 aa,如果 kk 当前还比解法数 xx 大,说明最终答案一定不在 aa 里,把 kk 变成 k−xk-x,继续下一轮枚举即可。否则,说明答案一定是 aa 开头的第 kk 个数,即 k+max⁡(10B−1,10C−1−i)−1k+\max(10^{B-1},10^{C-1}-i)-1,停止枚举,直接输出即可。如果枚举完了还没答案,那只能说明无解,输出 −1-1 了。

代码

C++
# include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int t, a, b, c, lg[10], x;
ll k;
int main () {
	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
	cin >> t;
	while (t --) {
		cin >> a >> b >> c >> k;
		x = max (a, b);
		if (c != x && c != x + 1) { //不可能有解啊
			cout << "-1\n";
			continue ;
		}
		for (int i = lg[a - 1]; i < lg[a]; ++ i) {
			x = max (0, min (lg[b], lg[c] - i) - max (lg[b - 1], lg[c - 1] - i));
			if (k > x) {
				k -= x;
				continue ;
			}
			k += max (lg[b - 1], lg[c - 1] - i) - 1;
			cout << i << " + " << k << " = " << i + k << '\n';
			goto there; //使程序跳转到 there 的地方继续执行
		}
		cout << "-1\n";
there:
		;
	}
	return 0;
}
原文