結果

問題 No.10 +か×か
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-07 01:00:48
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 101 ms / 5,000 ms
コード長 1,113 bytes
コンパイル時間 1,232 ms
コンパイル使用メモリ 77,528 KB
実行使用メモリ 78,952 KB
最終ジャッジ日時 2023-08-21 06:13:32
合計ジャッジ時間 2,619 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
76,364 KB
testcase_01 AC 74 ms
76,404 KB
testcase_02 AC 74 ms
76,264 KB
testcase_03 AC 101 ms
78,624 KB
testcase_04 AC 82 ms
78,952 KB
testcase_05 AC 74 ms
76,324 KB
testcase_06 AC 85 ms
78,480 KB
testcase_07 AC 80 ms
78,532 KB
testcase_08 AC 78 ms
78,540 KB
testcase_09 AC 74 ms
76,228 KB
testcase_10 AC 75 ms
76,620 KB
testcase_11 AC 73 ms
76,772 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 xrange(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 xrange(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