結果

問題 No.10 +か×か
ユーザー codershifth
提出日時 2016-08-30 01:38:44
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 75 ms / 5,000 ms
コード長 1,468 bytes
コンパイル時間 1,506 ms
コンパイル使用メモリ 168,904 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-14 15:37:35
合計ジャッジ時間 2,242 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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 Yuki0010
{
public:
    void solve(void)
    {
            int N,Total;
            cin>>N>>Total;

            vector<int> A(N);
            REP(i,N) cin>>A[i];

            vector<vector<bool>> vis(N+1, vector<bool>(Total+1, false));
            string ans;
            function<bool(int,int,string)> dfs = [&](int s, int k, string str)
            {
                if ( s > Total )
                    return false;
                if (k == N-1)
                {
                    if ( s == Total )
                    {
                        ans = str;
                        return true;
                    }
                    return false;
                }
                if ( vis[k][s] )
                    return false;
                vis[k][s] = true;
                if ( dfs(s+A[k+1], k+1, str+'+') )
                    return true;
                if ( dfs(s*A[k+1], k+1, str+'*') )
                    return true;
                return false;
            };
            dfs(A[0], 0, "");
            cout<<ans<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new Yuki0010();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0