結果

問題 No.10 +か×か
ユーザー tnakao0123tnakao0123
提出日時 2016-03-02 10:41:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 1,285 bytes
コンパイル時間 1,412 ms
コンパイル使用メモリ 83,136 KB
実行使用メモリ 42,604 KB
最終ジャッジ日時 2023-08-21 06:17:54
合計ジャッジ時間 1,539 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
42,416 KB
testcase_01 AC 13 ms
42,508 KB
testcase_02 AC 12 ms
42,412 KB
testcase_03 AC 28 ms
42,504 KB
testcase_04 AC 16 ms
42,604 KB
testcase_05 AC 12 ms
42,492 KB
testcase_06 AC 29 ms
42,388 KB
testcase_07 AC 17 ms
42,428 KB
testcase_08 AC 15 ms
42,412 KB
testcase_09 AC 15 ms
42,440 KB
testcase_10 AC 12 ms
42,420 KB
testcase_11 AC 12 ms
42,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 10.cc: No.10 +か×か - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 50;
const int MAX_T = 100000;

/* typedef */

typedef long long ll;

/* global variables */

int as[MAX_N];
ll dp[MAX_N][MAX_T + 1];

/* subroutines */

/* main */

int main() {
  int n, t;
  cin >> n >> t;

  for (int i = 0; i < n; i++) cin >> as[i];

  memset(dp, -1, sizeof(dp));
  dp[0][as[0]] = 0;

  for (int i = 0; i < n - 1; i++)
    for (int j = 0; j <= t; j++)
      if (dp[i][j] >= 0) {
	int add = j + as[i + 1];
	ll dpa = (dp[i][j] << 1);
	if (add <= t &&
	    (dp[i + 1][add] < 0 || dp[i + 1][add] > dpa))
	  dp[i + 1][add] = dpa;

	int mul = j * as[i + 1];
	ll dpm = ((dp[i][j] << 1) | 1);
	if (mul <= t &&
	    (dp[i + 1][mul] < 0 || dp[i + 1][mul] > dpm))
	  dp[i + 1][mul] = dpm;
      }

  ll ans = dp[n - 1][t];
  for (int i = n - 2; i >= 0; i--)
    putchar((ans & (1LL << i)) == 0 ? '+' : '*');
  putchar('\n');

  return 0;
}
0