結果

問題 No.2165 Let's Play Nim!
ユーザー a01sa01to
提出日時 2025-10-02 00:05:24
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 1,103 bytes
コンパイル時間 2,654 ms
コンパイル使用メモリ 284,152 KB
実行使用メモリ 26,228 KB
平均クエリ数 863.56
最終ジャッジ日時 2025-10-02 00:05:45
合計ジャッジ時間 20,086 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int sum = 0;
  rep(i, n) sum ^= a[i];
  vector<set<int>> pos(30);
  rep(i, n) {
    for (int j = 29; j >= 0; --j) {
      if (a[i] & (1 << j)) pos[j].insert(i);
    }
  }
  cout << (sum ? 1 : 0) << endl;
  while (true) {
    int i, k;
    if (sum == 0) {
      cin >> i >> k;
      --i;
    }
    else {
      for (int j = 29; j >= 0; --j) {
        if (sum & (1 << j)) {
          i = *pos[j].begin();
          break;
        }
      }
      // a[i] - k = (sum ^ a[i]);
      k = a[i] - (sum ^ a[i]);
      assert(k <= a[i]);
      cout << i + 1 << ' ' << k << endl;
    }
    sum ^= a[i];
    for (int j = 29; j >= 0; --j) {
      if (a[i] & (1 << j)) pos[j].erase(i);
    }
    a[i] -= k;
    sum ^= a[i];
    for (int j = 29; j >= 0; --j) {
      if (a[i] & (1 << j)) pos[j].insert(i);
    }
    int res;
    cin >> res;
    if (res == -1) return 0;
  }
  return 0;
}
0