結果

問題 No.10 +か×か
ユーザー tempura-samatempura-sama
提出日時 2016-04-13 23:15:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 760 bytes
コンパイル時間 375 ms
コンパイル使用メモリ 51,176 KB
実行使用メモリ 12,468 KB
最終ジャッジ日時 2023-08-21 06:18:04
合計ジャッジ時間 1,183 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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

	// 経路を記憶し、辿りつけないとわかっている時はスキップするようにしないとタイムアウトする
	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