結果

問題 No.10 +か×か
ユーザー masa
提出日時 2015-01-19 14:43:48
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 887 bytes
コンパイル時間 596 ms
コンパイル使用メモリ 62,604 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2024-12-14 15:26:41
合計ジャッジ時間 1,137 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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