結果

問題 No.15 カタログショッピング
ユーザー 👑 colognecologne
提出日時 2022-01-21 13:19:08
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 117 ms / 5,000 ms
コード長 772 bytes
コンパイル時間 205 ms
コンパイル使用メモリ 82,136 KB
実行使用メモリ 84,808 KB
最終ジャッジ日時 2024-05-04 02:01:42
合計ジャッジ時間 1,399 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
52,536 KB
testcase_01 AC 33 ms
52,432 KB
testcase_02 AC 34 ms
52,732 KB
testcase_03 AC 38 ms
58,780 KB
testcase_04 AC 32 ms
53,356 KB
testcase_05 AC 109 ms
84,744 KB
testcase_06 AC 117 ms
84,712 KB
testcase_07 AC 111 ms
84,696 KB
testcase_08 AC 108 ms
84,572 KB
testcase_09 AC 115 ms
84,808 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