题解:P16228 [蓝桥杯 2026 省 A] 读取指令
xiaohuanghuang · · 题解
P16228 [蓝桥杯 2026 省 A] 读取指令题解
我们首先理清题目的核心逻辑:
每个扇区
最少的区间数不可能超过 2:
- 如果存在长度为
k 的连续区间总和等于S ,那么答案就是 1。 - 如果不存在单个区间满足条件,我们总能找到两个不重叠的连续区间,通过调整两端数字的大小,凑出总和为
S ,此时答案就是 2。
还要注意特判
AC code
#include <bits/stdc++.h>//Code by xiaohuanghuang
using namespace std;
int t, n, c, w;
int main() {
cin >> t;
while (t--) {
cin >> n >> c >> w;
if (!w) {
cout << "0\n";
continue;
}
long long tot = 1LL * c * n * (n + 1) / 2;
if (tot < w || w % c) {
cout << "-1\n";
continue;
}
long long s = w / c, sum = 0;
bool f = 0;
for (int l = 1, r = 1; r <= n; r++) {
sum += r;
while (sum > s)sum -= l++;
if (sum == s) {
f = 1;
break;
}
}
cout << (f ? "1\n" : "2\n");
}
return 0;
}
本题解使用了 deepseek-v4-pro 进行润色以保证格式的正确。