結果

問題 No.10 +か×か
ユーザー onexisanonexisan
提出日時 2015-05-20 11:17:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 1,027 bytes
コンパイル時間 1,226 ms
コンパイル使用メモリ 145,148 KB
実行使用メモリ 5,048 KB
最終ジャッジ日時 2023-08-21 06:12:33
合計ジャッジ時間 1,883 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
template<class T>bool chmin(T&a,const T&b){return a>b?a=b,1:0;}

const int N = 50;
const int M = 100100;

int n, total, a[N];

void input()
{
    cin >> n >> total;
    rep(i, n) cin >> a[i];
}

const long inf = 1L << 60;

long dp[2][M];

void solve()
{
    int cur = 0, suc = 1;
    fill_n(dp[cur], total + 1, inf);
    dp[cur][a[0]] = 0;
    repi(i, 1, n) {
        fill_n(dp[suc], total + 1, inf);
        rep(x, total + 1) if (dp[cur][x] != inf) {
            chmin(dp[suc][x + a[i]], dp[cur][x]);
            const int nx = x * a[i];
            if (nx <= total) {
                chmin(dp[suc][nx], dp[cur][x] | 1L << (n - i - 1));
            }
        }
        swap(cur, suc);
    }
    for (int i = n - 2; i >= 0; --i) {
        cout << "+*"[dp[cur][total] >> i & 1];
    }
    cout << endl;
}

int main()
{
    cin.tie(0);
    ios_base::sync_with_stdio(false);

    input();
    solve();
}
0