結果

問題 No.10 +か×か
ユーザー warashi
提出日時 2015-10-25 18:42:57
言語 Go
(1.23.4)
結果
TLE  
実行時間 -
コード長 686 bytes
コンパイル時間 10,955 ms
コンパイル使用メモリ 236,664 KB
実行使用メモリ 15,012 KB
最終ジャッジ日時 2024-10-10 20:05:42
合計ジャッジ時間 17,276 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 3 TLE * 1 -- * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
)

var N, TOTAL int
var A []int

type in struct {
	i, total int
}
type out struct {
	can bool
	m   string
}

var cache = make(map[in]out)

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

	can, str := calc(1, A[0], "")
	if can {
		fmt.Println(str)
	}
}

func calc(i, n int, m string) (bool, string) {
	if c, ok := cache[in{i, n}]; ok {
		return c.can, c.m
	}
	if i == N {
		if n == TOTAL {
			return true, m
		} else {
			return false, m
		}
	}

	p, ps := calc(i+1, n+A[i], m+"+")
	if p {
		return true, ps
	}
	mul, muls := calc(i+1, n*A[i], m+"*")
	if mul {
		return true, muls
	}
	return false, m
}
0