結果

問題 No.10 +か×か
ユーザー codershifthcodershifth
提出日時 2015-07-13 00:15:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 37 ms / 5,000 ms
コード長 2,020 bytes
コンパイル時間 1,246 ms
コンパイル使用メモリ 151,916 KB
実行使用メモリ 10,228 KB
最終ジャッジ日時 2023-08-21 06:12:50
合計ジャッジ時間 2,089 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 37 ms
10,228 KB
testcase_04 AC 24 ms
7,844 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 37 ms
10,136 KB
testcase_07 AC 26 ms
7,824 KB
testcase_08 AC 10 ms
5,636 KB
testcase_09 AC 18 ms
8,736 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 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<bool>> dp(Total+1, vector<bool>(N+1, false));
            dp[Total][N] = true;  // start mark

            // O(Total*N) <= 50*10^5 = 5*10^6
            for (int i = N-1; i >= 0; --i)
            REP(s, Total+1)
            {
                if (!dp[s][i+1])
                    continue;
                if (s-A[i] >= 0)
                    dp[s-A[i]][i] = true;
                if (s%A[i] == 0)
                    dp[s/A[i]][i] = true;
            }
            // 順方向でパスを作成する。
            // 作成できないパスは途中でとまっているはず
            //                      N
            //                +++++++ <- stop
            //           +++++**+++++ <- stop
            //  +...+++++*++++**+++++ <- reach
            //  +...+**+++*+++++*++++ <- reach
            //
            string ans;
            int    s = A[0];
            FOR(i, 1, N)
            {
                if (s+A[i] <= Total && dp[s+A[i]][i+1])
                {
                    ans += "+";
                    s += A[i];
                }
                else
                {
                    ans += "*";
                    s *= A[i];
                }
            }
            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