結果

問題 No.10 +か×か
ユーザー ふーらくたるふーらくたる
提出日時 2017-02-17 02:39:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 962 bytes
コンパイル時間 382 ms
コンパイル使用メモリ 52,980 KB
実行使用メモリ 9,276 KB
最終ジャッジ日時 2023-08-21 06:21:40
合計ジャッジ時間 1,093 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,276 KB
testcase_01 AC 5 ms
9,184 KB
testcase_02 AC 5 ms
9,180 KB
testcase_03 AC 11 ms
9,204 KB
testcase_04 AC 9 ms
9,268 KB
testcase_05 AC 5 ms
9,268 KB
testcase_06 AC 11 ms
9,192 KB
testcase_07 AC 10 ms
9,188 KB
testcase_08 AC 6 ms
9,212 KB
testcase_09 AC 7 ms
9,228 KB
testcase_10 AC 5 ms
9,200 KB
testcase_11 AC 5 ms
9,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstring>
using namespace std;

const int MAX_N = 60;
const int MAX_TOTAL = 100010;

int a[MAX_N];

bool dp[MAX_N][MAX_TOTAL];

int main() {
    int n, total;
    cin >> n >> total;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    memset(dp, false, sizeof(dp));

    dp[n][total] = true;
    for (int i = n - 1; i >= 0; i--) {
        for (int tot = 0; tot <= total; tot++) {
            if (tot + a[i] <= total) {
                dp[i][tot] |= dp[i + 1][tot + a[i]];
            }
            if (tot * a[i] <= total) {
                dp[i][tot] |= dp[i + 1][tot * a[i]];
            }
        }
    }
    int idx = 1,
        cur_total = a[0];
    while (idx < n) {
        if (dp[idx + 1][cur_total + a[idx]]) {
            cout << "+";
            cur_total += a[idx];
        } else {
            cout << "*";
            cur_total *= a[idx];
        }
        idx++;
    }
    cout << endl;

    return 0;
}
0