結果

問題 No.10 +か×か
ユーザー furafura
提出日時 2020-01-02 18:15:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 225 ms / 5,000 ms
コード長 879 bytes
コンパイル時間 1,630 ms
コンパイル使用メモリ 168,804 KB
実行使用メモリ 8,200 KB
最終ジャッジ日時 2023-08-21 06:26:49
合計ジャッジ時間 2,905 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 225 ms
8,180 KB
testcase_04 AC 73 ms
7,340 KB
testcase_05 AC 2 ms
5,568 KB
testcase_06 AC 210 ms
8,200 KB
testcase_07 AC 84 ms
7,360 KB
testcase_08 AC 15 ms
6,336 KB
testcase_09 AC 21 ms
5,304 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

bool solve(const vector<int>& a,int total){
	int n=a.size();
	static bool dp[50][100001];
	rep(i,n) rep(x,total+1) dp[i][x]=false;
	dp[0][a[0]]=true;
	rep(i,n-1) rep(x,total+1) if(dp[i][x]) {
		if(x+a[i+1]<=total) dp[i+1][x+a[i+1]]=true;
		if(x*a[i+1]<=total) dp[i+1][x*a[i+1]]=true;
	}
	return dp[n-1][total];
}

int main(){
	int n,total; scanf("%d%d",&n,&total);
	vector<int> a(n);
	rep(i,n) scanf("%d",&a[i]);

	string ans;
	rep(i,n-1){
		for(char c:{'+','*'}){
			ans+=c;

			bool ok=true;
			vector<int> b(n-i-1);
			b[0]=a[0];
			rep(j,i+1){
				if(ans[j]=='+') b[0]+=a[j+1];
				else            b[0]*=a[j+1];
				if(b[0]>total){ ok=false; break; }
			}
			rep(j,n-i-2) b[j+1]=a[j+i+2];

			if(ok && solve(b,total)) break;

			ans.pop_back();
		}
	}
	puts(ans.c_str());

	return 0;
}
0