結果

問題 No.2165 Let's Play Nim!
ユーザー t33ft33f
提出日時 2022-12-17 01:37:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 180 ms / 2,000 ms
コード長 1,684 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 91,212 KB
実行使用メモリ 25,580 KB
平均クエリ数 889.67
最終ジャッジ日時 2024-04-28 00:00:36
合計ジャッジ時間 14,302 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
25,196 KB
testcase_01 AC 133 ms
24,812 KB
testcase_02 AC 145 ms
24,812 KB
testcase_03 AC 91 ms
24,940 KB
testcase_04 AC 21 ms
24,812 KB
testcase_05 AC 26 ms
25,196 KB
testcase_06 AC 27 ms
24,812 KB
testcase_07 AC 25 ms
25,196 KB
testcase_08 AC 97 ms
24,568 KB
testcase_09 AC 20 ms
25,208 KB
testcase_10 AC 30 ms
24,824 KB
testcase_11 AC 136 ms
24,812 KB
testcase_12 AC 121 ms
24,812 KB
testcase_13 AC 20 ms
24,940 KB
testcase_14 AC 25 ms
25,196 KB
testcase_15 AC 20 ms
25,052 KB
testcase_16 AC 114 ms
25,580 KB
testcase_17 AC 63 ms
25,196 KB
testcase_18 AC 180 ms
24,812 KB
testcase_19 AC 20 ms
25,208 KB
testcase_20 AC 25 ms
24,824 KB
testcase_21 AC 52 ms
25,220 KB
testcase_22 AC 42 ms
24,836 KB
testcase_23 AC 39 ms
25,076 KB
testcase_24 AC 35 ms
24,836 KB
testcase_25 AC 78 ms
25,220 KB
testcase_26 AC 134 ms
25,196 KB
testcase_27 AC 50 ms
25,196 KB
testcase_28 AC 79 ms
25,196 KB
testcase_29 AC 20 ms
24,812 KB
testcase_30 AC 19 ms
25,196 KB
testcase_31 AC 18 ms
25,196 KB
testcase_32 AC 67 ms
25,196 KB
evil_2_big_1.txt AC 1,383 ms
24,940 KB
evil_2_big_2.txt AC 1,631 ms
24,812 KB
evil_2_big_3.txt AC 1,350 ms
25,324 KB
evil_big_1.txt AC 1,611 ms
24,836 KB
evil_big_2.txt AC 1,652 ms
25,220 KB
evil_big_3.txt AC 1,633 ms
25,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class State {
    int x = 0;
    array<unordered_set<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].insert(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(i);
        if (const int v = oval - k; v > 0)
            for (int b = 0; b < 17; ++b)
                if (v >> b & 1) elems[b].insert(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 int i = *elems[l].begin();
        const int v = iv[i];
        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