結果

問題 No.10 +か×か
コンテスト
ユーザー なお
提出日時 2014-10-11 06:00:38
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 634 bytes
コンパイル時間 456 ms
コンパイル使用メモリ 55,572 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2024-12-14 15:25:00
合計ジャッジ時間 1,065 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdlib.h>
#include <iostream>
#include <string>
using namespace std;
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))

int N, total, A[51];
bool memo[51][100100];
string s;

void dfs(int i, int num){
    if(num > total) return;
    if(memo[i][num]) return;
    if(i == N){
        if(num == total){ cout << s << endl; exit(0); }
        return;
    }
    s.push_back('+'); dfs(i+1, num + A[i]); s.pop_back();
    s.push_back('*'); dfs(i+1, num * A[i]); s.pop_back();
    memo[i][num] = true;
}

int main(){
    cin >> N >> total;
    REP(i,N) cin >> A[i];
    dfs(1,A[0]);
    return 0;
}
0