結果

問題 No.10 +か×か
ユーザー kk
提出日時 2014-10-30 10:19:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,100 bytes
コンパイル時間 712 ms
コンパイル使用メモリ 77,916 KB
実行使用メモリ 23,380 KB
最終ジャッジ日時 2023-08-21 06:07:49
合計ジャッジ時間 1,404 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
23,208 KB
testcase_01 AC 9 ms
23,380 KB
testcase_02 AC 8 ms
23,204 KB
testcase_03 AC 13 ms
23,284 KB
testcase_04 AC 8 ms
23,212 KB
testcase_05 AC 8 ms
23,332 KB
testcase_06 AC 15 ms
23,212 KB
testcase_07 AC 10 ms
23,216 KB
testcase_08 AC 9 ms
23,276 KB
testcase_09 AC 9 ms
23,172 KB
testcase_10 AC 9 ms
23,340 KB
testcase_11 AC 8 ms
23,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <string>
#include <cstring>
#include <map>

using namespace std;

int memo[51][100020];

int total;
vector <int> a;
vector <char> ans;
bool rec(int i, int val) {
  if (memo[i][val] != -1) return memo[i][val];
  if (i == a.size()-1) {
    if (val == total) {
      return true;
    }
    return false;
  }

  if (val+a[i+1] <= total && rec(i+1, val+a[i+1])) {
    ans.push_back('+');
    return memo[i][val]=true;
  }
  if (val*a[i+1] <= total && rec(i+1, val*a[i+1])) {
    ans.push_back('*');
    return memo[i][val]=true;
  }

  return memo[i][val]=false;
}

int main() {
  memset(memo, -1, sizeof(memo));
  int n;
  cin >> n;
  cin >> total;
  for (int i = 0; i < n; i++) {
    int x; cin >> x; a.push_back(x);
  }

  rec(0, a[0]);

  for (int i = ans.size()-1; i >= 0; i--) cout << ans[i];
  cout << endl;
}
0