思路
题目中有句很重要的话:
Each input file has at most test cases which do not satisfy .
这意味着 的做法是可行的。
我们可以枚举每个 ,枚举范围是 。对于每个 对应的解法就是 ,解法数就是 种。对于每个 ,如果 当前还比解法数 大,说明最终答案一定不在 里,把 变成 ,继续下一轮枚举即可。否则,说明答案一定是 开头的第 个数,即 ,停止枚举,直接输出即可。如果枚举完了还没答案,那只能说明无解,输出 了。
代码
# 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;
}