結果

問題 No.10 +か×か
ユーザー しらっ亭しらっ亭
提出日時 2015-06-20 03:57:16
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 986 ms / 5,000 ms
コード長 1,167 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 10,856 KB
実行使用メモリ 48,224 KB
最終ジャッジ日時 2023-08-21 06:13:02
合計ジャッジ時間 4,866 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,192 KB
testcase_01 AC 16 ms
8,244 KB
testcase_02 AC 16 ms
8,244 KB
testcase_03 AC 986 ms
48,212 KB
testcase_04 AC 728 ms
34,440 KB
testcase_05 AC 16 ms
8,196 KB
testcase_06 AC 967 ms
48,224 KB
testcase_07 AC 730 ms
34,412 KB
testcase_08 AC 199 ms
15,876 KB
testcase_09 AC 351 ms
22,476 KB
testcase_10 AC 20 ms
8,516 KB
testcase_11 AC 17 ms
8,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# dp[i, j] は、 i 番目までつかって j の値のとき、残りの数値で t を作れるかが入ってる

def solve(N, T, A):
    dp = [[False] * (T + 1) for i in range(N + 1)]
    for j in range(T):
        dp[N][j] = False  # 全部使ってしまったら作れない
    dp[N][T] = True  # 全部つかって t は作れる
    for i in range(N - 1, -1, -1):
        for j in range(T + 1):
            dp[i][j] = False
            if j + A[i] <= T and dp[i + 1][j + A[i]]:
                dp[i][j] = True
            if j * A[i] <= T and dp[i + 1][j * A[i]]:
                dp[i][j] = 1

    ans = []
    j = A[0]
    for i in range(1, N):
        if j + A[i] <= T and dp[i + 1][ j + A[i]]:
            ans.append('+')
            j += A[i]
            continue
        if j * A[i] <= T and dp[i + 1][ j * A[i]]:
            ans.append('*')
            j *= A[i]
            continue

    return ''.join(ans)


def main():
    n = int(input())
    total = int(input())
    a = list(map(int, input().split()))
    print(solve(n, total, a))


if __name__ == '__main__':
    main()

# TLE するので解説に載ってるやつでお勉強しました
0