結果

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

テストケース

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

ソースコード

diff #

#include <iostream>
using namespace std;

#define N 50

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

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][tn] == -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][tn] = 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