結果

問題 No.10 +か×か
ユーザー motimoti
提出日時 2015-07-25 00:49:48
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,127 bytes
コンパイル時間 1,524 ms
コンパイル使用メモリ 77,224 KB
実行使用メモリ 29,080 KB
最終ジャッジ日時 2023-09-23 03:58:20
合計ジャッジ時間 7,342 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
24,884 KB
testcase_01 AC 8 ms
24,804 KB
testcase_02 AC 8 ms
24,832 KB
testcase_03 AC 20 ms
25,008 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

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 constexpr Max = 100001;
int dp[Max][55];

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

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

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

  if(now+A[idx] < Max && dfs(now+A[idx], idx+1)) {
    ope = 0; return true;
  }
  if(now*A[idx] < Max && 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