結果

問題 No.2165 Let's Play Nim!
ユーザー t33ft33f
提出日時 2022-12-17 01:22:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 195 ms / 2,000 ms
コード長 1,687 bytes
コンパイル時間 781 ms
コンパイル使用メモリ 87,464 KB
実行使用メモリ 24,276 KB
平均クエリ数 880.41
最終ジャッジ日時 2023-08-10 06:34:16
合計ジャッジ時間 16,739 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
23,952 KB
testcase_01 AC 149 ms
24,156 KB
testcase_02 AC 148 ms
23,940 KB
testcase_03 AC 104 ms
23,976 KB
testcase_04 AC 25 ms
23,808 KB
testcase_05 AC 28 ms
23,316 KB
testcase_06 AC 30 ms
23,388 KB
testcase_07 AC 27 ms
23,616 KB
testcase_08 AC 104 ms
24,024 KB
testcase_09 AC 24 ms
23,592 KB
testcase_10 AC 35 ms
23,940 KB
testcase_11 AC 145 ms
24,024 KB
testcase_12 AC 129 ms
23,640 KB
testcase_13 AC 24 ms
24,012 KB
testcase_14 AC 29 ms
23,604 KB
testcase_15 AC 26 ms
23,604 KB
testcase_16 AC 127 ms
23,580 KB
testcase_17 AC 72 ms
23,472 KB
testcase_18 AC 195 ms
23,316 KB
testcase_19 AC 24 ms
24,276 KB
testcase_20 AC 32 ms
23,952 KB
testcase_21 AC 58 ms
23,484 KB
testcase_22 AC 49 ms
24,252 KB
testcase_23 AC 44 ms
23,316 KB
testcase_24 AC 43 ms
24,264 KB
testcase_25 AC 96 ms
23,580 KB
testcase_26 AC 149 ms
23,400 KB
testcase_27 AC 51 ms
23,424 KB
testcase_28 AC 77 ms
23,952 KB
testcase_29 AC 23 ms
23,604 KB
testcase_30 AC 24 ms
23,832 KB
testcase_31 AC 24 ms
23,832 KB
testcase_32 AC 126 ms
24,240 KB
evil_2_big_1.txt AC 1,886 ms
23,472 KB
evil_2_big_2.txt AC 1,888 ms
23,316 KB
evil_2_big_3.txt AC 1,793 ms
24,084 KB
evil_big_1.txt AC 1,935 ms
23,436 KB
evil_big_2.txt AC 1,911 ms
23,316 KB
evil_big_3.txt AC 1,867 ms
23,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <set>
#include <array>
#include <vector>
#include <iostream>
using namespace std;

class State {
    int x = 0;
    array<set<pair<int, int>>, 17> elems;
    vector<int> iv;
public:
    State(int n) : iv(n) {};
    void add(int i, int v) {
        x ^= v;
        for (int b = 0; b < 17; ++b)
            if (v >> b & 1) elems[b].emplace(v, i);
        iv[i] = v;
    }
    int sum() const { return x; }
    void remove(int i, int k) {
        const int oval = iv[i];
        for (int b = 0; b < 17; ++b)
            if (oval >> b & 1) elems[b].erase(make_pair(oval, i));
        if (const int v = oval - k; v > 0)
            for (int b = 0; b < 17; ++b)
                if (v >> b & 1) elems[b].insert(make_pair(v, i));
        iv[i] -= k;
        x ^= oval ^ iv[i];
    }
    pair<int, int> next_move() {
        assert(x != 0);
        const int l = 31 - __builtin_clz(x);
        assert(x >> l & 1);
        assert((x >> (l + 1)) == 0);
        const auto [v, i] = *elems[l].begin();
        assert(v > (v ^ x));
        return make_pair(i, v - (v ^ x));
    }
};

int main() {
    int n; cin >> n;
    State s(n);
    
    for (int i = 0; i < n; ++i) {
        int a; cin >> a;
        s.add(i, a);
    }
    cout << int(s.sum() != 0) << endl;
    for (;;) {
        const int x = s.sum();
        cerr << "x = " << x << endl;
        if (x == 0) {
            int i, k; cin >> i >> k;
            i--;
            s.remove(i, k);
        } else {
            const auto [i, k] = s.next_move();
            s.remove(i, k);
            cout << i + 1 << ' ' << k << endl;
        
        }
        int r; cin >> r;
        if (r == -1) return 0;
    }
}
0