結果

問題 No.10 +か×か
ユーザー troro_kelp
提出日時 2018-12-30 23:30:31
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 1,039 bytes
コンパイル時間 848 ms
コンパイル使用メモリ 94,600 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-12-14 15:41:31
合計ジャッジ時間 1,491 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <stack>
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <functional>
#include <numeric>
using ll = long long;
using namespace std;

const ll MOD = 1e9 + 7;
int N, Total;
int A[51];
int dp[51][100100];
string S;
// bool operator<(const pair<int, int> &a, const pair<int, int> &b)
// {
//     if (a.first == b.first)
//         return a.second < b.second;
//     return a.first < b.first;
// };

void dfs(int cur, int v)
{
    if (v > Total)
        return;
    if (cur == N)
    {
        if (v == Total)
        {
            cout << S << endl;
            exit(0);
        }
        return;
    }
    if (dp[cur][v])
        return;
    dp[cur][v] = 1;

    S += "+";
    dfs(cur + 1, v + A[cur]);
    S.pop_back();
    S += "*";
    dfs(cur + 1, v * A[cur]);
    S.pop_back();
}
int main(void)
{

    cin >> N >> Total;
    for (int i = 0; i < N; ++i)
        cin >> A[i];

    dfs(1, A[0]);
    return 0;
}
0