結果

問題 No.10 +か×か
ユーザー warashiwarashi
提出日時 2015-10-25 19:18:13
言語 Go
(1.22.1)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,293 bytes
コンパイル時間 10,962 ms
コンパイル使用メモリ 197,908 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 06:15:16
合計ジャッジ時間 11,737 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 5 ms
4,384 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"strings"
)

func main() {
	var N, TOTAL int
	var A []int
	fmt.Scan(&N, &TOTAL)
	A = make([]int, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&A[i])
	}
	fmt.Println(solve(N, TOTAL, A))
}

func solve(N, total int, A []int) string {
	dp := make([]map[int]bool, N)
	for i := 0; i < N; i++ {
		dp[i] = make(map[int]bool)
	}
	dp[0][A[0]] = true
	dp[len(dp)-1][total] = true
	backcast(dp, A)
	return forcast(dp, A)
}
func backcast(dp []map[int]bool, A []int) {
	for i := len(dp) - 1; i > 0; i-- {
		dpn := dp[i-1]
		a := A[i]
		for b := range dp[i] {
			if b > a {
				dpn[b-a] = true
			}
			if b%a == 0 {
				dpn[b/a] = true
			}
		}
	}
}

func forcast(dp []map[int]bool, A []int) string {
	pos := 1
	val := A[0]
	path := make([]string, 0, len(dp)-1)
	dfs(val, pos, &path, dp, A)
	return strings.Join(path, "")
}

func dfs(val int, pos int, path *[]string, dp []map[int]bool, A []int) bool {
	if pos == len(dp) {
		return true
	}
	a := A[pos]
	if _, ok := dp[pos][val+a]; ok {
		*path = append(*path, "+")
		if dfs(val+a, pos+1, path, dp, A) {
			return true
		}
		*path = (*path)[:len(*path)-1]
	}
	if _, ok := dp[pos][val*a]; ok {
		*path = append(*path, "*")
		if dfs(val*a, pos+1, path, dp, A) {
			return true
		}
		*path = (*path)[:len(*path)-1]
	}
	return false
}
0