結果
| 問題 | No.10 +か×か |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-01-02 18:15:00 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 238 ms / 5,000 ms |
| コード長 | 879 bytes |
| 記録 | |
| コンパイル時間 | 1,406 ms |
| コンパイル使用メモリ | 166,364 KB |
| 実行使用メモリ | 8,556 KB |
| 最終ジャッジ日時 | 2025-12-06 14:53:09 |
| 合計ジャッジ時間 | 2,638 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 13 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:20:27: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
20 | int n,total; scanf("%d%d",&n,&total);
| ~~~~~^~~~~~~~~~~~~~~~~~
main.cpp:22:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
22 | rep(i,n) scanf("%d",&a[i]);
| ~~~~~^~~~~~~~~~~~
ソースコード
#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;
}