結果

問題 No.10 +か×か
ユーザー motimoti
提出日時 2015-07-25 01:17:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 1,167 bytes
コンパイル時間 560 ms
コンパイル使用メモリ 74,816 KB
実行使用メモリ 25,068 KB
最終ジャッジ日時 2023-08-21 06:13:23
合計ジャッジ時間 1,329 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
24,844 KB
testcase_01 AC 8 ms
24,804 KB
testcase_02 AC 8 ms
24,856 KB
testcase_03 AC 14 ms
24,888 KB
testcase_04 AC 9 ms
25,032 KB
testcase_05 AC 8 ms
24,888 KB
testcase_06 AC 18 ms
25,028 KB
testcase_07 AC 13 ms
24,808 KB
testcase_08 AC 9 ms
25,068 KB
testcase_09 AC 9 ms
24,848 KB
testcase_10 AC 8 ms
24,848 KB
testcase_11 AC 8 ms
24,844 KB
権限があれば一括ダウンロードができます

ソースコード

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;
int A[55];//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 == 2) { return false; }
  if(ope >= 0) { 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;
  }
  ope = 2;
  return false;
}

int main() {

  memset(dp, -1, sizeof dp);

  cin >> N >> tot;
//  A.resize(N);
  rep(i, N) {
    cin >> A[i];
  }

  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