結果

問題 No.10 +か×か
ユーザー kroton
提出日時 2014-11-01 04:41:47
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 201 ms / 5,000 ms
コード長 1,048 bytes
コンパイル時間 543 ms
コンパイル使用メモリ 58,792 KB
実行使用メモリ 177,796 KB
最終ジャッジ日時 2024-12-14 15:25:30
合計ジャッジ時間 2,949 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

const string dame = "dame";

bool done[55][100010];
string dp[55][100010];

int N, total, A[55];
string solve(int k, int acc){
    if(k >= N){
        if(acc == total){
            return "";
        } else {
            return dame;
        }
    }
    if(acc > total)return dame;
    if(done[k][acc])return dp[k][acc];
    
    {
        string next = solve(k + 1, acc + A[k]);
        if(next != dame){
            string res = "+" + next;
            
            done[k][acc] = true;
            return dp[k][acc] = res;
        }
    }
    
    {
        string next = solve(k + 1, acc * A[k]);
        if(next != dame){
            string res = "*" + next;
            
            done[k][acc] = true;
            return dp[k][acc] = res;
        }
    }
    
    done[k][acc] = true;
    return dp[k][acc] = dame;
}


int main(){
    cin >> N >> total;
    for(int i=0;i<N;i++)cin >> A[i];
    
    cout << solve(1, A[0]) << endl;
    return 0;
}
0