結果

問題 No.15 カタログショッピング
ユーザー 👑 colognecologne
提出日時 2022-01-21 13:19:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 165 ms / 5,000 ms
コード長 772 bytes
コンパイル時間 614 ms
コンパイル使用メモリ 87,212 KB
実行使用メモリ 86,092 KB
最終ジャッジ日時 2023-08-16 18:26:38
合計ジャッジ時間 2,297 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,384 KB
testcase_01 AC 73 ms
71,524 KB
testcase_02 AC 74 ms
71,500 KB
testcase_03 AC 77 ms
75,500 KB
testcase_04 AC 71 ms
71,564 KB
testcase_05 AC 161 ms
85,792 KB
testcase_06 AC 163 ms
85,804 KB
testcase_07 AC 165 ms
85,912 KB
testcase_08 AC 165 ms
85,900 KB
testcase_09 AC 160 ms
86,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
import sys

input = sys.stdin.readline


def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(len(s)+1))


def main():
    N, S = map(int, input().split())
    P = [int(input()) for i in range(N)]

    FH = {}
    for x in powerset(range(N//2)):
        s = sum(P[i] for i in x)
        if s not in FH:
            FH[s] = []
        FH[s].append(x)

    ans = []

    for x in powerset(range(N//2, N)):
        s = sum(P[i] for i in x)
        for v in FH.get(S-s, []):
            ans.append(v+x)

    for i in sorted(ans):
        print(*map(lambda x: x+1, i))


if __name__ == '__main__':
    main()
0