結果
問題 |
No.10 +か×か
|
ユーザー |
|
提出日時 | 2020-01-02 18:15:00 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 227 ms / 5,000 ms |
コード長 | 879 bytes |
コンパイル時間 | 1,631 ms |
コンパイル使用メモリ | 169,596 KB |
実行使用メモリ | 8,388 KB |
最終ジャッジ日時 | 2024-12-14 15:43:04 |
合計ジャッジ時間 | 2,860 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 12 |
ソースコード
#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; }