結果

問題 No.10 +か×か
ユーザー kyuna
提出日時 2020-05-02 01:00:41
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 702 bytes
コンパイル時間 802 ms
コンパイル使用メモリ 78,664 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-14 15:44:08
合計ジャッジ時間 1,401 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n; cin >> n;
    int total; cin >> total;
    vector<int> a(n);
    for (auto &ai: a) cin >> ai;
    string ans;
    vector<vector<bool>> dp(n, vector<bool>(total + 1));
    auto dfs = [&](auto dfs, int i, int res) {
        if (res > total) return ;
        if (i == n) {
            if (res == total) cout << ans << endl, exit(0);
            return ;
        }
        if (dp[i][res]) return ;
        dp[i][res] = true;
        ans += '+', dfs(dfs, i + 1, res + a[i]), ans.pop_back();
        ans += '*', dfs(dfs, i + 1, res * a[i]), ans.pop_back();
    };
    dfs(dfs, 1, a[0]);
    return 0;
}
0