結果

問題 No.10 +か×か
ユーザー tempura-samatempura-sama
提出日時 2016-04-13 23:13:14
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 769 bytes
コンパイル時間 384 ms
コンパイル使用メモリ 54,112 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 01:08:18
合計ジャッジ時間 932 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
using namespace std;

#define N 50

int n;
int ans;
int v[N];
char op[N-1];
int na[N][100001];

int dfs(char o, int l, int t)
{
	int tn = o == '+' ? (t + v[l + 1]) : (t * v[l + 1]);
	op[l] = o;
	if ( na[l][t] == -1 || tn > ans ) {
		return -1;
	}
	if ( l == n - 2 ) {
		return tn == ans ? 1 : -1;
	}
	int x = dfs('+', l + 1, tn);
	if ( x != 1 ) {
		x = dfs('*', l + 1, tn);
	}

	// 経路を記憶し、辿りつけないとわかっている時はスキップするようにしないとタイムアウトする
	na[l][t] = x;

	return x;
}

int main()
{
	cin >> n;
	cin >> ans;
	for ( int i = 0; i < n; i++ ) {
		cin >> v[i];
	}

	dfs('+', 0, v[0]) <= 0 && dfs('*', 0, v[0]);

	for ( int i = 0; i < n - 1; i++ ) {
		cout << op[i];
	}

	return 0;
}
0