結果

問題 No.10 +か×か
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-07 00:59:09
言語 Python2
(2.7.18)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 1,111 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 6,708 KB
実行使用メモリ 8,152 KB
最終ジャッジ日時 2023-08-21 06:13:29
合計ジャッジ時間 903 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,972 KB
testcase_01 AC 11 ms
5,908 KB
testcase_02 AC 11 ms
5,932 KB
testcase_03 AC 19 ms
8,152 KB
testcase_04 AC 16 ms
6,824 KB
testcase_05 AC 12 ms
5,912 KB
testcase_06 AC 19 ms
7,860 KB
testcase_07 AC 14 ms
6,540 KB
testcase_08 AC 12 ms
6,080 KB
testcase_09 AC 11 ms
6,016 KB
testcase_10 AC 11 ms
5,876 KB
testcase_11 AC 11 ms
6,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    N = int(raw_input())
    total = int(raw_input())
    A = list(map(int, raw_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