結果

問題 No.10 +か×か
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-03 15:57:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 9 ms / 5,000 ms
コード長 943 bytes
コンパイル時間 1,743 ms
コンパイル使用メモリ 166,288 KB
実行使用メモリ 17,992 KB
最終ジャッジ日時 2023-08-21 06:29:50
合計ジャッジ時間 2,471 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 9 ms
12,928 KB
testcase_04 AC 8 ms
17,992 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 9 ms
16,580 KB
testcase_07 AC 8 ms
17,992 KB
testcase_08 AC 4 ms
11,820 KB
testcase_09 AC 5 ms
9,776 KB
testcase_10 AC 2 ms
5,400 KB
testcase_11 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int memo[51][100010];
int main() {
    int N, Total, A[60];
    cin >> N >> Total;
    for (int i = 0; i < N; i++)
        cin >> A[i];
    memo[N - 1][Total] = 1;

    for (int i = N - 2; i >= 0; i--) {
        for (int x = 0; x <= Total; x++) {
            if (memo[i + 1][x]) {
                if (x - A[i + 1] >= 0)
                    memo[i][x - A[i + 1]] = 1;
                if (x % A[i + 1] == 0 && !memo[i][x / A[i + 1]]) {
                    memo[i][x / A[i + 1]] = 2;
                }
            }
        }
    }

    string ans;
    int tot = A[0];
    for (int i = 0; i < N - 1; i++) {
        if (memo[i][tot] == 1) {
            ans.push_back('+');
            tot += A[i + 1];
        } else if (memo[i][tot] == 2) {
            ans.push_back('*');
            tot *= A[i + 1];
        } else {
            cerr << "error" << endl;
        }
    }
    cout << ans << endl;
}
0