結果

問題 No.10 +か×か
ユーザー codershifth
提出日時 2015-07-12 18:51:56
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,717 bytes
コンパイル時間 1,156 ms
コンパイル使用メモリ 165,136 KB
実行使用メモリ 11,776 KB
最終ジャッジ日時 2024-07-08 05:56:28
合計ジャッジ時間 1,717 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 7 WA * 5
権限があれば一括ダウンロードができます

ソースコード

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] == 's'))
                    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