結果

問題 No.10 +か×か
ユーザー moti
提出日時 2015-07-25 00:47:19
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
RE  
実行時間 -
コード長 1,061 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 84,996 KB
実行使用メモリ 31,500 KB
最終ジャッジ日時 2024-07-16 04:07:08
合計ジャッジ時間 7,338 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 3 RE * 1 TLE * 1 -- * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <set>
#include <map>
#include <assert.h>
#include <cstring>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;

int N, tot;
vector<int> A;
int dp[100010][55];

bool dfs(int now, int idx) {
  auto& ope = dp[now][idx];

  if(idx == N) { return now == tot; }

  if(ope != -1) { return true; }

  if(dfs(now+A[idx], idx+1)) {
    ope = 0; return true;
  }
  if(dfs(now*A[idx], idx+1)) {
    ope = 1; return true;
  }
  return false;
}

int main() {

  memset(dp, -1, sizeof dp);
  dp[0][0] = 0;

  cin >> N >> tot;
  rep(i, N) {
    int a; cin >> a;
    A.push_back(a);
  }

  assert(dfs(A[0], 1));

  std::string ans;
  int num = A[0];
  REP(i, 1, N) {
    if(dp[num][i] == 0) {
      ans += '+';
      num += A[i];
    }
    else if(dp[num][i] == 1) {
      ans += '*';
      num *= A[i];
    }
    else {
      assert(false);
    }
  }
  cout << ans << endl;

  return 0;
}
0