結果

問題 No.2165 Let's Play Nim!
ユーザー trineutrontrineutron
提出日時 2022-12-16 08:19:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 202 ms / 2,000 ms
コード長 1,399 bytes
コンパイル時間 2,062 ms
コンパイル使用メモリ 203,836 KB
最終ジャッジ日時 2025-02-09 14:16:01
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    int s = 0;
    vector<unordered_set<int>> on(17);

    auto add = [&](int i) {
        for (int j = 0; j < 17; j++) {
            if (a.at(i) >> j & 1) {
                on.at(j).insert(i);
            }
        }
    };

    auto rm = [&](int i) {
        for (int j = 0; j < 17; j++) {
            on.at(j).erase(i);
        }
    };

    for (int i = 0; i < n; i++) {
        cin >> a.at(i);
        s ^= a.at(i);
        add(i);
    }

    auto ans = [&]() {
        int top = -1;
        for (int i = 0; i < 17; i++) {
            if (s >> i & 1) {
                top = i;
            }
        }
        int idx = *on.at(top).begin();
        cout << idx + 1 << ' ' << a.at(idx) - (a.at(idx) ^ s) << endl;
        a.at(idx) ^= s;
        s = 0;
        rm(idx);
        add(idx);
        int ret;
        cin >> ret;
        if (ret == -1) {
            exit(0);
        }
    };

    if (s) {
        cout << 1 << endl;
        ans();
    } else {
        cout << 0 << endl;
    }

    for (;;) {
        int i, k;
        cin >> i >> k;
        i--;
        s ^= a.at(i);
        a.at(i) -= k;
        s ^= a.at(i);
        rm(i);
        add(i);
        int ret;
        cin >> ret;
        if (ret == -1) {
            return 0;
        }
        ans();
    }

    return 0;
}
0