結果

問題 No.15 カタログショッピング
ユーザー cologne
提出日時 2022-01-21 13:19:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 119 ms / 5,000 ms
コード長 772 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 82,400 KB
実行使用メモリ 84,960 KB
最終ジャッジ日時 2024-11-25 07:56:31
合計ジャッジ時間 1,566 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

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