結果

問題 No.10 +か×か
ユーザー simezi_tansimezi_tan
提出日時 2015-07-21 18:02:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 1,065 bytes
コンパイル時間 1,232 ms
コンパイル使用メモリ 144,632 KB
実行使用メモリ 25,456 KB
最終ジャッジ日時 2023-08-21 06:13:28
合計ジャッジ時間 1,903 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
25,316 KB
testcase_01 AC 9 ms
25,148 KB
testcase_02 AC 9 ms
25,136 KB
testcase_03 AC 12 ms
25,200 KB
testcase_04 AC 9 ms
25,268 KB
testcase_05 AC 8 ms
25,316 KB
testcase_06 AC 14 ms
25,376 KB
testcase_07 AC 11 ms
25,456 KB
testcase_08 AC 9 ms
25,268 KB
testcase_09 AC 9 ms
25,364 KB
testcase_10 AC 8 ms
25,164 KB
testcase_11 AC 9 ms
25,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define mp make_pair
#define pb push_back
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define dbg(x) cerr<<__LINE__<<": "<<#x<<" = "<<(x)<<endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

const int MX = 110000;
int n, total, a[50], dp[51][MX];

int rec(int c, int s){
	if(c == n) return s == total ? 0 : inf;
	if(dp[c][s] != -1) return dp[c][s];
	
	if(s + a[c] < MX && rec(c + 1, s + a[c]) < inf) return dp[c][s] = 0;
	if(s * a[c] < MX && rec(c + 1, s * a[c]) < inf) return dp[c][s] = 1;
	return dp[c][s] = inf;
}

int main(){
	cin >> n >> total;
	rep(i, n) cin >> a[i];
	
	memset(dp, -1, sizeof(dp));
	rec(1, a[0]);
	
	string ans;
	int s = a[0];
	rep(i, n - 1){
		
		if(dp[i + 1][s] == 0){
			ans += "+";
			s += a[i + 1];
		}
		else{
			ans += "*";
			s *= a[i + 1];
		}
	}
	cout << ans << endl;
	
	return 0;
}
0