結果
問題 |
No.10 +か×か
|
ユーザー |
![]() |
提出日時 | 2014-12-09 19:37:59 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,455 bytes |
コンパイル時間 | 1,603 ms |
コンパイル使用メモリ | 160,476 KB |
実行使用メモリ | 9,884 KB |
最終ジャッジ日時 | 2024-06-11 18:56:36 |
合計ジャッジ時間 | 8,430 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 7 TLE * 1 -- * 4 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i,n) repi(i,0,n) #define repi(i,a,b) for(int i=int(a);i<int(b);++i) #define long int64_t #define ulong uint64_t const int N = 50; int n, total, a[N]; void input() { cin >> n >> total; rep(i, n) cin >> a[i]; } const int inf = 1000000; int min_result(int i, int cur) { repi(j, i, n) { if (a[j] > 1) cur += a[j]; cur = min(cur, inf); } return cur; } int max_result(int i, int cur) { repi(j, i, n) { if (a[j] == 1) ++cur; else cur *= a[j]; cur = min(cur, inf); } return cur; } bool dfs(int i, int cur) { if (i == n) return cur == total; if (total < min_result(i, cur) or max_result(i, cur) < total) return false; return dfs(i + 1, cur + a[i]) or dfs(i + 1, cur * a[i]); } void solve() { string tmp; while (n and total % a[n - 1] != 0) { tmp.push_back('+'); total -= a[--n]; } reverse(tmp.begin(), tmp.end()); string ans; int cur = a[0]; repi(i, 1, n) { if (dfs(i + 1, cur + a[i])) { ans.push_back('+'), cur += a[i]; } else { ans.push_back('*'), cur *= a[i]; } } ans.insert(ans.end(), tmp.begin(), tmp.end()); cout << ans << endl; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); input(); solve(); }