結果

問題 No.10 +か×か
ユーザー kk
提出日時 2014-10-30 10:34:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 964 bytes
コンパイル時間 584 ms
コンパイル使用メモリ 72,704 KB
実行使用メモリ 23,508 KB
最終ジャッジ日時 2023-08-21 06:07:59
合計ジャッジ時間 1,463 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
23,308 KB
testcase_01 AC 8 ms
23,312 KB
testcase_02 AC 8 ms
23,400 KB
testcase_03 AC 12 ms
23,340 KB
testcase_04 AC 9 ms
23,376 KB
testcase_05 AC 9 ms
23,448 KB
testcase_06 AC 14 ms
23,332 KB
testcase_07 AC 11 ms
23,308 KB
testcase_08 AC 8 ms
23,508 KB
testcase_09 AC 9 ms
23,400 KB
testcase_10 AC 8 ms
23,328 KB
testcase_11 AC 8 ms
23,380 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;
int n;
int a[51];
char ans[50];
bool rec(int i, int val) {
  if (memo[i][val] != -1) return memo[i][val];
  if (i == n-1) {
    return (val == total);
  }

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

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

int main() {
  memset(memo, -1, sizeof(memo));
  scanf ("%d", &n);
  scanf ("%d", &total);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);

  rec(0, a[0]);
  printf ("%s\n", ans);
}
0