結果

問題 No.10 +か×か
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-07 00:54:41
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 24 ms / 5,000 ms
コード長 1,099 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 11,036 KB
実行使用メモリ 10,864 KB
最終ジャッジ日時 2023-08-21 06:13:24
合計ジャッジ時間 1,011 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,212 KB
testcase_01 AC 16 ms
8,308 KB
testcase_02 AC 16 ms
8,268 KB
testcase_03 AC 24 ms
10,864 KB
testcase_04 AC 20 ms
9,392 KB
testcase_05 AC 16 ms
8,156 KB
testcase_06 AC 22 ms
10,600 KB
testcase_07 AC 18 ms
8,720 KB
testcase_08 AC 17 ms
8,348 KB
testcase_09 AC 16 ms
8,392 KB
testcase_10 AC 16 ms
8,232 KB
testcase_11 AC 16 ms
8,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    N = int(input())
    total = int(input())
    A = list(map(int, input().split()))
    return N, total, A

def solve(N, total, A):
    dp = [set() for i in range(N)]
    dp[0].add(A[0])
    dp[-1].add(total)
    backcast(dp, N, A)
    path = forecast(dp, N, A)
    return path

def backcast(dp, N, A):
    for i in range(N-1, 0, -1):
        dpn = dp[i-1]
        a = A[i]
        for b in dp[i]:
            if b > a:
                dpn.add(b - a)
            if b % a == 0:
                dpn.add(b // a)

def forecast(dp, N, A):
    pos = 1
    val = A[0]
    path = []
    dfs(val, pos, path, dp, A)
    return ''.join(path)

def dfs(val, pos, path, dp, A):
    if pos == len(dp):
        return True
    a = A[pos]
    if val + a in dp[pos]:
        path.append('+')
        if dfs(val + a, pos + 1, path, dp, A):
            return True
        del path[-1]
    if val * a in dp[pos]:
        path.append('*')
        if dfs(val * a, pos + 1, path, dp, A):
            return True
        del path[-1]
    return False

N, total, A = read_data()
print(solve(N, total, A))
0