Dmaxiya

[蓝桥杯 2026 省 A] 读取指令 参考代码

Apr 11th, 2026
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5.  
  6. typedef long long LL;
  7. const int maxn = 100000 + 100;
  8. int T;
  9. LL n, c, w;
  10. LL sum[maxn];
  11.  
  12. void init() {
  13.     for (int i = 1; i < maxn; ++i) {
  14.         sum[i] = sum[i - 1] + i;
  15.     }
  16. }
  17.  
  18. int main() {
  19.     ios::sync_with_stdio(false);
  20.  
  21.     init();
  22.     cin >> T;
  23.     while (T--) {
  24.         cin >> n >> c >> w;
  25.         if (w % c != 0) {
  26.             cout << -1 << endl;
  27.             continue;
  28.         }
  29.         if (w == 0) {
  30.             cout << 0 << endl;
  31.             continue;
  32.         }
  33.         w /= c;
  34.         LL sumn = (1 + n) * n / 2;
  35.         if (w > sumn) {
  36.             cout << -1 << endl;
  37.             continue;
  38.         }
  39.         bool flag = false;
  40.         for (LL i = 0; i <= n; ++i) {
  41.             int idx = upper_bound(sum, sum + n + 1, w + sum[i]) - sum - 1;
  42.             if (sum[idx] - sum[i] == w) {
  43.                 flag = true;
  44.                 break;
  45.             }
  46.         }
  47.         if (flag) {
  48.             cout << 1 << endl;
  49.         } else {
  50.             cout << 2 << endl;
  51.         }
  52.     }
  53.  
  54.     return 0;
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment