結果

問題 No.10 +か×か
コンテスト
ユーザー KKT89
提出日時 2026-07-12 01:30:19
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 8 ms / 5,000 ms
+ 306µs
コード長 1,196 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,644 ms
コンパイル使用メモリ 337,352 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2026-07-12 01:30:26
合計ジャッジ時間 4,339 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 13
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, total;
    cin >> n >> total;

    vector<int> a(n);
    for (int& x : a) cin >> x;

    // dp[i][x]:
    // a[0] ... a[i] まで計算した結果が x のとき、
    // 残りを使って total にできるか
    vector<vector<char>> dp(n, vector<char>(total + 1));

    dp[n - 1][total] = true;

    for (int i = n - 2; i >= 0; --i) {
        for (int x = 1; x <= total; ++x) {
            int add = x + a[i + 1];
            if (add <= total and dp[i + 1][add]) {
                dp[i][x] = true;
            }

            long long mul = 1LL * x * a[i + 1];
            if (mul <= total and dp[i + 1][mul]) {
                dp[i][x] = true;
            }
        }
    }

    string ans;
    int cur = a[0];

    for (int i = 0; i + 1 < n; ++i) {
        int add = cur + a[i + 1];

        // '+' で完成可能なら辞書順のために優先する
        if (add <= total and dp[i + 1][add]) {
            ans += '+';
            cur = add;
        } else {
            ans += '*';
            cur *= a[i + 1];
        }
    }

    cout << ans << '\n';
}
0