結果

問題 No.10 +か×か
ユーザー PachicobuePachicobue
提出日時 2017-05-29 02:11:06
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 1,997 bytes
コンパイル時間 1,488 ms
コンパイル使用メモリ 165,512 KB
実行使用メモリ 42,484 KB
最終ジャッジ日時 2023-08-21 06:22:57
合計ジャッジ時間 2,304 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,344 KB
testcase_01 AC 3 ms
7,460 KB
testcase_02 AC 3 ms
7,436 KB
testcase_03 AC 26 ms
42,328 KB
testcase_04 AC 18 ms
42,348 KB
testcase_05 AC 18 ms
42,384 KB
testcase_06 AC 28 ms
42,484 KB
testcase_07 AC 20 ms
42,404 KB
testcase_08 AC 14 ms
23,800 KB
testcase_09 AC 11 ms
21,836 KB
testcase_10 AC 4 ms
9,516 KB
testcase_11 AC 5 ms
9,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define pii pair<int, int>
#define vi vector<int>

using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p)
{
    return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc)
{
    o << "sz = " << vc.size() << endl
      << "[";
    for (const T& v : vc)
        o << v << ",";
    o << "]";
    return o;
}
using ll = long long;
constexpr ll MOD = 1000000007;
constexpr ll MAX = 50;
constexpr ll VMAX = 100000;
ll num[MAX];
ll dp[MAX][VMAX + 1];

string decode(ll ope)
{
    string str;
    while (ope > 1) {
        str.push_back((ope % 2 == 0) ? '+' : '*');
        ope /= 2;
    }
    reverse(str.begin(), str.end());
    return str;
}

int main()
{
    ll n;
    cin >> n;
    ll total;
    cin >> total;
    rep(i, n)
    {
        cin >> num[i];
    }

    rep(i, n)
    {
        rep(v, VMAX + 1)
        {
            dp[i][v] = numeric_limits<ll>::max();
        }
    }

    dp[0][num[0]] = 1;
    rep1(i, n - 1)
    {
        rep(v, VMAX + 1)
        {
            if (dp[i - 1][v] != numeric_limits<ll>::max()) {
                if (v + num[i] <= VMAX) {
                    dp[i][v + num[i]] = min(dp[i][v + num[i]], 2 * dp[i - 1][v]);
                }
                if (v * num[i] <= VMAX) {
                    dp[i][v * num[i]] = min(dp[i][v * num[i]], 2 * dp[i - 1][v] + 1);
                }
            }
        }
    }

    cout << decode(dp[n - 1][total]) << endl;
    return 0;
}
0