結果

問題 No.10 +か×か
ユーザー atn112323atn112323
提出日時 2016-05-04 03:46:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 805 bytes
コンパイル時間 564 ms
コンパイル使用メモリ 52,224 KB
実行使用メモリ 12,576 KB
最終ジャッジ日時 2023-08-21 06:18:49
合計ジャッジ時間 1,313 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <string>

using namespace std;

int N, total;
int A[50];
int memo[50][100001];

bool dfs(int idx, int v) {
	if (v > total) {
		return false;
	}
	if (idx == N) {
		if (v == total) {
			return true;
		} else {
			return false;
		}
	}
	if (memo[idx][v] != 0) {
		return memo[idx][v] == 1;
	}
	if (dfs(idx + 1, v + A[idx])) {
		memo[idx][v] = 1;
		return true;
	}
	if (dfs(idx + 1, v * A[idx])) {
		memo[idx][v] = 1;
		return true;
	}
	memo[idx][v] = -1;
	return false;
}

int main() {
	cin >> N >> total;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	string res;
	int v = A[0];
	for (int i = 1; i < N; i++) {
		if (dfs(i + 1, v + A[i])) {
			res += "+";
			v += A[i];
		} else if (dfs(i + 1, v * A[i])) {
			res += "*";
			v *= A[i];
		}
	}
	cout << res << endl;
	return 0;
}
0