結果

問題 No.10 +か×か
ユーザー codershifthcodershifth
提出日時 2015-07-12 18:47:19
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,672 bytes
コンパイル時間 1,409 ms
コンパイル使用メモリ 150,956 KB
実行使用メモリ 11,796 KB
最終ジャッジ日時 2023-09-22 14:21:04
合計ジャッジ時間 2,255 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 2 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 12 ms
7,560 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class AddOrMul {
public:
    void solve(void) {
            int N, Total;
            cin>>N>>Total;
            vector<int> A(N);
            REP(i, N)
                cin>>A[i];

            // dp[i][s] = i 番目まで見たときに s の値を作成するのに使った最後演算子
            vector<vector<char>> dp(Total+1, vector<char>(N+1, ' '));
            dp[A[0]][1] = 's';  // start mark
            // O(Total*N) <= 50*10^5 = 5*10^6
            FOR(i, 1, N)
            REP(s, Total+1)
            {
                if (dp[s][i] == ' ')
                    continue;
                if (s+A[i] <= Total)
                    dp[s+A[i]][i+1] = '+';
                if (s*A[i] <= Total && dp[s*A[i]][i+1] == ' ') // '+' で表現できるならそれを利用する
                    dp[s*A[i]][i+1] = '*';
            }
            string ans;
            REP(i, N-1)
            {
                if (dp[Total][N-i] == '+')
                {
                    Total -= A[N-i-1];
                    ans += "+";
                }
                else
                {
                    Total /= A[N-i-1];
                    ans += "*";
                }
            }
            reverse(RANGE(ans));
            cout<<ans<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new AddOrMul();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0