結果

問題 No.15 カタログショッピング
ユーザー H3PO4H3PO4
提出日時 2021-02-06 08:54:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 147 ms / 5,000 ms
コード長 913 bytes
コンパイル時間 534 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 91,064 KB
最終ジャッジ日時 2023-09-15 13:32:47
合計ジャッジ時間 2,665 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,528 KB
testcase_01 AC 95 ms
71,804 KB
testcase_02 AC 92 ms
71,452 KB
testcase_03 AC 146 ms
77,292 KB
testcase_04 AC 93 ms
71,744 KB
testcase_05 AC 142 ms
91,012 KB
testcase_06 AC 142 ms
90,796 KB
testcase_07 AC 147 ms
90,928 KB
testcase_08 AC 144 ms
91,064 KB
testcase_09 AC 144 ms
90,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

N, S = map(int, input().split())
P = [int(input()) for _ in range(N)]


def full_search(X):
    L = len(X)
    table = [0] * (1 << L)
    for b in range(1 << L):
        for i in range(L):
            if b & (1 << i):
                table[b] = table[b ^ (1 << i)] + X[i]
    return table


lsum, rsum = full_search(P[:N // 2]), full_search(P[N // 2:])
r_dict = defaultdict(list)
for b, x in enumerate(rsum):
    r_dict[x].append(b)
r_dict = dict(r_dict)


def decode(b, l):
    for i in range(l):
        if b & (1 << i):
            yield i + 1


ans = []
for bl, x in enumerate(lsum):
    if S - x in r_dict:
        for br in r_dict[S - x]:
            tmp = list(decode(bl, N // 2))
            for x in decode(br, N - N // 2):
                tmp.append(x + N // 2)
            tmp = tuple(sorted(tmp))
            ans.append(tmp)
ans.sort()
for t in ans:
    print(*t)
0