結果

問題 No.10 +か×か
ユーザー kk
提出日時 2014-10-30 09:42:05
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,127 bytes
コンパイル時間 738 ms
コンパイル使用メモリ 88,248 KB
実行使用メモリ 7,692 KB
最終ジャッジ日時 2023-08-28 23:14:28
合計ジャッジ時間 7,439 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 74 ms
4,380 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

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 <map>

using namespace std;

map <vector <char>, bool> memo;

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

  p.push_back('+');
  if (rec(p, val+a[p.size()])) {
    p.pop_back();
    return memo[p]=true;
  }
  p.pop_back();
  p.push_back('*');
  if (rec(p, val*a[p.size()])) {
    p.pop_back();
    return memo[p]=true;
  }

  return false;
}

int main() {
  int n;
  cin >> n;
  cin >> total;
  for (int i = 0; i < n; i++) {
    int x; cin >> x; a.push_back(x);
  }

  vector <char> p;
  rec(p, a[0]);

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