結果

問題 No.10 +か×か
ユーザー masamasa
提出日時 2015-01-19 14:43:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 887 bytes
コンパイル時間 497 ms
コンパイル使用メモリ 63,372 KB
実行使用メモリ 5,644 KB
最終ジャッジ日時 2023-08-21 06:09:43
合計ジャッジ時間 1,169 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

int n, total;
vector<int> a;
string ans;
bool fail[50][100000];

bool calc(int result, int idx) {
	if (idx == n) {
		// cout << ans << " " << result << endl;
		if (result == total) {
			return true;
		}
		return false;
	}
	if (fail[idx][result]) {
		return false;
	}

	for (int i = 0; i < 2; i++) {
		int next;
		if (i == 0) {
			ans[idx - 1] = '+';
			next = result + a[idx];
		} else {
			ans[idx - 1] = '*';
			next = result * a[idx];
		}
		if (next <= total) {
			if (calc(next, idx + 1)) {
				return true;
			} else {
				fail[idx][result] = true;
			}
		}
	}
	return false;
}

int main() {
	cin >> n;
	cin >> total;

	a.assign(n, 0);
	ans.assign(n - 1, '?');

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

	calc(a[0], 1);
	cout << ans << endl;
	return 0;
}
0