結果

問題 No.15 カタログショッピング
ユーザー szkhtsszkhts
提出日時 2021-02-23 14:37:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 436 ms / 5,000 ms
コード長 905 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 11,932 KB
実行使用メモリ 16,028 KB
最終ジャッジ日時 2023-10-23 14:58:45
合計ジャッジ時間 3,880 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,256 KB
testcase_01 AC 28 ms
10,256 KB
testcase_02 AC 28 ms
10,260 KB
testcase_03 AC 32 ms
10,264 KB
testcase_04 AC 29 ms
10,256 KB
testcase_05 AC 426 ms
16,028 KB
testcase_06 AC 428 ms
16,028 KB
testcase_07 AC 428 ms
16,028 KB
testcase_08 AC 436 ms
16,028 KB
testcase_09 AC 423 ms
16,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, S = map(int, input().split())

price = [int(input()) for _ in range(N)]

price_A = {}

a = N // 2
b = N - a

for i in range(1 << a):
    sum = 0
    for j in range(a):
        if (i >> j) % 2 == 0:
            continue
        sum += price[j]
    if sum in price_A:
        price_A[sum].append(i)
    else:
        price_A[sum] = [i]
 
answer = []
for i in range(1 << b):
    sum = 0
    for j in range(b):
        if (i >> j) % 2 == 0:
            continue
        sum += price[a + j]
    need = S - sum
    if need in price_A:
        for ar in price_A[need]:
            ans = []
            for j in range(a):
                if (ar >> j) % 2 == 1:
                    ans.append(j + 1)
            for j in range(b):
                if (i >> j) % 2 == 1:
                    ans.append(a + j + 1)
            answer.append(ans)

answer.sort()

for ans in answer:
    print(' '.join(map(str, ans)))
0