結果

問題 No.184 たのしい排他的論理和(HARD)
ユーザー 草苺奶昔草苺奶昔
提出日時 2023-03-15 14:14:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 233 ms / 5,000 ms
コード長 2,268 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 81,656 KB
実行使用メモリ 97,088 KB
最終ジャッジ日時 2023-10-18 12:23:31
合計ジャッジ時間 8,065 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
68,164 KB
testcase_01 AC 62 ms
68,164 KB
testcase_02 AC 62 ms
68,164 KB
testcase_03 AC 61 ms
68,164 KB
testcase_04 AC 63 ms
68,164 KB
testcase_05 AC 63 ms
68,164 KB
testcase_06 AC 63 ms
68,164 KB
testcase_07 AC 63 ms
68,164 KB
testcase_08 AC 207 ms
89,244 KB
testcase_09 AC 103 ms
78,748 KB
testcase_10 AC 162 ms
84,860 KB
testcase_11 AC 139 ms
84,128 KB
testcase_12 AC 201 ms
93,696 KB
testcase_13 AC 211 ms
94,804 KB
testcase_14 AC 154 ms
84,920 KB
testcase_15 AC 225 ms
96,840 KB
testcase_16 AC 200 ms
91,976 KB
testcase_17 AC 207 ms
94,264 KB
testcase_18 AC 65 ms
68,584 KB
testcase_19 AC 61 ms
68,172 KB
testcase_20 AC 102 ms
89,760 KB
testcase_21 AC 233 ms
97,088 KB
testcase_22 AC 228 ms
97,060 KB
testcase_23 AC 69 ms
70,792 KB
testcase_24 AC 66 ms
68,724 KB
testcase_25 AC 67 ms
70,784 KB
testcase_26 AC 63 ms
68,172 KB
testcase_27 AC 66 ms
68,724 KB
testcase_28 AC 168 ms
86,340 KB
testcase_29 AC 208 ms
94,172 KB
testcase_30 AC 191 ms
91,072 KB
testcase_31 AC 177 ms
88,172 KB
testcase_32 AC 201 ms
92,036 KB
testcase_33 AC 226 ms
96,864 KB
testcase_34 AC 223 ms
96,800 KB
testcase_35 AC 229 ms
97,004 KB
testcase_36 AC 226 ms
96,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from typing import List


class LinearBase:
    __slots__ = ("bases", "_rows", "_bit")

    @staticmethod
    def fromlist(nums: List[int]) -> "LinearBase":
        res = LinearBase(bit=max(nums, default=0).bit_length())
        for x in nums:
            res.add(x)
        res.build()
        return res

    def __init__(self, bit=62):
        self.bases = []  # 基底
        self._rows = defaultdict(int)  # 高斯消元的行
        self._bit = bit  # 最大数的位数

    def add(self, x: int) -> bool:
        """插入一个向量,如果插入成功返回True,否则返回False"""
        x = self._normalize(x)
        if x == 0:
            return False
        i = x.bit_length() - 1
        for j in range(self._bit):
            if (self._rows[j] >> i) & 1:
                self._rows[j] ^= x
        self._rows[i] = x
        return True

    def build(self) -> None:
        res = []
        for _, v in sorted(self._rows.items()):
            if v > 0:
                res.append(v)
        self.bases = res

    def kthXor(self, k: int) -> int:
        """子序列(子集,包含空集)第k小的异或 1<=k<=2**len(self.bases)"""
        assert 1 <= k <= 2 ** len(self.bases)
        k -= 1
        res = 0
        for i in range(k.bit_length()):
            if (k >> i) & 1:
                res ^= self.bases[i]
        return res

    def maxXor(self) -> int:
        return self.kthXor(2 ** len(self.bases))

    def _normalize(self, x: int) -> int:
        for i in range(x.bit_length() - 1, -1, -1):
            if (x >> i) & 1:
                x ^= self._rows[i]
        return x

    def __len__(self) -> int:
        return len(self.bases)

    def __contains__(self, x: int) -> bool:
        """x是否能由线性基表出"""
        if x == 0:
            return True
        bases = []
        for k in range(self._bit - 1, -1, -1):
            curX = x
            if self._rows[k] != 0:
                bases.append(self._rows[k])
                for b in bases:
                    curX = min(curX, curX ^ b)
                if curX == 0:
                    return True
        return False


n = int(input())
nums = list(map(int, input().split()))
print(2 ** len(LinearBase.fromlist(nums)))
0