結果

問題 No.1861 Required Number
ユーザー 👑 rin204rin204
提出日時 2023-06-21 22:01:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 183 ms / 2,500 ms
コード長 7,681 bytes
コンパイル時間 963 ms
コンパイル使用メモリ 86,932 KB
実行使用メモリ 83,768 KB
最終ジャッジ日時 2023-09-11 17:06:06
合計ジャッジ時間 18,721 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,128 KB
testcase_01 AC 73 ms
71,132 KB
testcase_02 AC 75 ms
71,324 KB
testcase_03 AC 167 ms
83,732 KB
testcase_04 AC 154 ms
83,768 KB
testcase_05 AC 74 ms
71,220 KB
testcase_06 AC 73 ms
71,184 KB
testcase_07 AC 78 ms
71,252 KB
testcase_08 AC 82 ms
70,904 KB
testcase_09 AC 77 ms
71,212 KB
testcase_10 AC 78 ms
71,096 KB
testcase_11 AC 77 ms
71,240 KB
testcase_12 AC 76 ms
71,108 KB
testcase_13 AC 77 ms
71,116 KB
testcase_14 AC 77 ms
71,216 KB
testcase_15 AC 78 ms
71,224 KB
testcase_16 AC 77 ms
71,088 KB
testcase_17 AC 77 ms
71,264 KB
testcase_18 AC 79 ms
71,116 KB
testcase_19 AC 87 ms
75,500 KB
testcase_20 AC 78 ms
71,112 KB
testcase_21 AC 76 ms
71,116 KB
testcase_22 AC 79 ms
71,124 KB
testcase_23 AC 75 ms
71,124 KB
testcase_24 AC 158 ms
80,100 KB
testcase_25 AC 170 ms
83,608 KB
testcase_26 AC 159 ms
80,480 KB
testcase_27 AC 170 ms
82,096 KB
testcase_28 AC 163 ms
81,948 KB
testcase_29 AC 168 ms
81,920 KB
testcase_30 AC 131 ms
81,536 KB
testcase_31 AC 172 ms
83,768 KB
testcase_32 AC 163 ms
81,904 KB
testcase_33 AC 174 ms
82,676 KB
testcase_34 AC 170 ms
82,964 KB
testcase_35 AC 175 ms
83,608 KB
testcase_36 AC 172 ms
81,772 KB
testcase_37 AC 166 ms
82,516 KB
testcase_38 AC 183 ms
82,964 KB
testcase_39 AC 168 ms
82,104 KB
testcase_40 AC 174 ms
83,748 KB
testcase_41 AC 173 ms
83,604 KB
testcase_42 AC 175 ms
83,756 KB
testcase_43 AC 169 ms
82,248 KB
testcase_44 AC 75 ms
71,184 KB
04_evil_1.txt MLE -
04_evil_2.txt MLE -
04_evil_3.txt MLE -
04_evil_4.txt MLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def popcount32(x):
    x = x - ((x >> 1) & 0x55555555)
    x = (x & 0x33333333) + ((x >> 2) & 0x33333333)
    x = (x + (x >> 4)) & 0x0F0F0F0F
    x += x >> 8
    x += x >> 16
    return x & 0x0000003F


def popcount64(x):
    x = (x & 0x5555555555555555) + ((x >> 1) & 0x5555555555555555)
    x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333)
    x = (x & 0x0F0F0F0F0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F0F0F0F0F)
    x = (x & 0x00FF00FF00FF00FF) + ((x >> 8) & 0x00FF00FF00FF00FF)
    x = (x & 0x0000FFFF0000FFFF) + ((x >> 16) & 0x0000FFFF0000FFFF)
    x = (x & 0x00000000FFFFFFFF) + ((x >> 32) & 0x00000000FFFFFFFF)
    return x


class Bitset:
    def __init__(self, n: int) -> None:
        self.n = n
        self.m = (n + 62) // 63
        self.A = [0] * self.m

    def __len__(self) -> int:
        return self.n

    @property
    def size(self) -> int:
        return self.n

    def __str__(self) -> str:
        S = []
        for a in self.A:
            S.append(bin(a)[2:].zfill(63)[::-1])
        S = "".join(S)
        return S[: self.n][::-1]

    def __getitem__(self, ind: int) -> int:
        i = ind // 63
        j = ind - i * 63
        return self.A[i] >> j & 1

    def __setitem__(self, ind: int, k: int) -> None:
        i = ind // 63
        j = ind - i * 63
        if (self.A[i] >> j & 1) != k:
            self.A[i] ^= 1 << j
        else:
            pass

    def rev(self, ind: int) -> None:
        i = ind // 63
        j = ind - i * 63
        self.A[i] ^= 1 << j

    def count(self) -> int:
        ret = 0
        for a in self.A:
            ret += popcount64(a)
        return ret

    def __sum__(self) -> int:
        return self.count()

    def resize(self, n: int) -> None:
        m = (n + 62) // 63
        if m > self.m:
            self.A += [0] * (m - self.m)
        else:
            self.A = self.A[:m]
            j = n % 63
            if j != 0:
                self.A[-1] &= (1 << j) - 1
            else:
                pass
        self.n = n
        self.m = m

    def __and__(self, other: "Bitset") -> "Bitset":
        if self.n < other.n:
            n = self.n
        else:
            n = other.n

        ret = Bitset(n)
        for i, (a, b) in enumerate(zip(self.A, other.A)):
            ret.A[i] = a & b

        return ret

    def __iand__(self, other: "Bitset") -> "Bitset":
        if self.m < other.m:
            m = self.m
        else:
            m = other.m
        for i in range(m):
            self.A[i] &= other.A[i]
        for i in range(m, self.m):
            self.A[i] = 0
        return self

    def __or__(self, other: "Bitset") -> "Bitset":
        if self.n > other.n:
            n = self.n
        else:
            n = other.n

        ret = Bitset(n)
        for i in range(ret.m):
            if i < self.m and i < other.m:
                ret.A[i] = self.A[i] | other.A[i]
            elif i < self.m:
                ret.A[i] = self.A[i]
            else:
                ret.A[i] = other.A[i]

        return ret

    def __ior__(self, other: "Bitset") -> "Bitset":
        if self.m < other.m:
            m = self.m
        else:
            m = other.m
        for i in range(m):
            self.A[i] |= other.A[i]
        if self.n < other.n:
            x = self.n % 63
            if x != 0:
                mask = (1 << x) - 1
                self.A[-1] &= mask
        else:
            pass
        return self

    def __xor__(self, other: "Bitset") -> "Bitset":
        if self.n > other.n:
            n = self.n
        else:
            n = other.n

        ret = Bitset(n)
        for i in range(ret.m):
            if i < self.m and i < other.m:
                ret.A[i] = self.A[i] ^ other.A[i]
            elif i < self.m:
                ret.A[i] = self.A[i]
            else:
                ret.A[i] = other.A[i]

        return ret

    def __ixor__(self, other: "Bitset") -> "Bitset":
        if self.m < other.m:
            m = self.m
        else:
            m = other.m
        for i in range(m):
            self.A[i] ^= other.A[i]
        if self.n < other.n:
            x = self.n % 63
            if x != 0:
                mask = (1 << x) - 1
                self.A[-1] &= mask
        else:
            pass
        return self

    def and_count(self, other: "Bitset") -> int:
        ret = 0
        for a, b in zip(self.A, other.A):
            ret += popcount64(a & b)
        return ret

    def or_count(self, other: "Bitset") -> int:
        ret = 0
        if self.m < other.m:
            m = self.m
        else:
            m = other.m
        for a, b in zip(self.A[:m], other.A[:m]):
            ret += popcount64(a | b)

        for a in self.A[m:]:
            ret += popcount64(a)

        for a in other.A[m:]:
            ret += popcount64(a)

        return ret

    def xor_count(self, other: "Bitset") -> int:
        ret = 0
        if self.m < other.m:
            m = self.m
        else:
            m = other.m
        for a, b in zip(self.A[:m], other.A[:m]):
            ret += popcount64(a ^ b)

        for a in self.A[m:]:
            ret += popcount64(a)

        for a in other.A[m:]:
            ret += popcount64(a)

        return ret

    def __rshift__(self, k: int) -> "Bitset":
        ret = Bitset(self.n)
        x = k // 63
        for i in range(x, self.m):
            ret.A[i - x] = self.A[i]
        k -= x * 63
        if k != 0:
            mask = (1 << k) - 1
            rk = 63 - k
            for i, a in enumerate(ret.A):
                if i != 0:
                    ret.A[i - 1] |= (a & mask) << (rk)
                ret.A[i] = a >> k
        else:
            pass
        return ret

    def __irshift__(self, k: int) -> "Bitset":
        x = k // 63
        for i in range(x, self.m):
            self.A[i - x] = self.A[i]
        for i in range(self.m - x, self.m):
            self.A[i] = 0
        k -= x * 63
        if k != 0:
            mask = (1 << k) - 1
            rk = 63 - k
            for i, a in enumerate(self.A):
                if i != 0:
                    self.A[i - 1] |= (a & mask) << (rk)
                self.A[i] = a >> k
        else:
            pass
        return self

    def __lshift__(self, k: int) -> "Bitset":
        ret = Bitset(self.n)
        x = k // 63
        for i in range(x, self.m):
            ret.A[i] = self.A[i - x]
        k -= x * 63
        if k != 0:
            rk = 63 - k
            mask = (1 << rk) - 1
            for i in range(self.m - 1, -1, -1):
                ret.A[i] &= mask
                ret.A[i] <<= k
                if i != 0:
                    ret.A[i] |= ret.A[i - 1] >> rk
        else:
            pass
        return ret

    def __ilshift__(self, k: int) -> "Bitset":
        x = k // 63
        for i in range(self.m - 1, x - 1, -1):
            self.A[i] = self.A[i - x]
        for i in range(x - 1, -1, -1):
            self.A[i] = 0
        k -= x * 63
        if k != 0:
            rk = 63 - k
            mask = (1 << rk) - 1
            for i in range(self.m - 1, -1, -1):
                self.A[i] &= mask
                self.A[i] <<= k
                if i != 0:
                    self.A[i] |= self.A[i - 1] >> rk
        else:
            pass
        return self


n, k = map(int, input().split())
A = list(map(int, input().split()))
dp = [Bitset(k + 1) for _ in range(n + 1)]
dp[0][0] = 1
for i, a in enumerate(A, 1):
    dp[i] = dp[i - 1] | (dp[i - 1] << a)

if not dp[n][k]:
    print(-1)
    exit()

bit = Bitset(k + 1)
bit[k] = 1
ans = 0
for i in range(n - 1, -1, -1):
    a = A[i]
    if bit.and_count(dp[i]) == 0:
        ans += 1
    bit |= bit >> a
print(ans)
0