結果

問題 No.10 +か×か
ユーザー 久我山菜々久我山菜々
提出日時 2018-09-30 23:20:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 1,182 bytes
コンパイル時間 1,003 ms
コンパイル使用メモリ 106,692 KB
実行使用メモリ 43,988 KB
最終ジャッジ日時 2023-08-21 06:24:38
合計ジャッジ時間 1,833 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 43 ms
43,988 KB
testcase_04 AC 23 ms
29,972 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 45 ms
43,748 KB
testcase_07 AC 25 ms
29,964 KB
testcase_08 AC 10 ms
10,972 KB
testcase_09 AC 15 ms
18,196 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<cmath>
#include<queue>
#include<numeric>
#include<bitset>

static int const INF{100000000};

int main(int, char**) {
  int n;
  std::cin >> n;
  int total;
  std::cin >> total;
  std::vector<int> as(n);
  for(int i{}; i < n; ++i) {
    std::cin >> as[i];
  }

  static long long const nil = 1LL << 60;
  std::vector<std::vector<long long>> dp(n + 1, std::vector<long long>(total + 1, nil));
  dp[0][as[0]] = 0;
  for(int i{1}; i < n; ++i) {
    for(int j{1}; j <= total; ++j) {
      if(dp[i - 1][j] == nil) continue;
      if(j + as[i] <= total) {
        dp[i][j + as[i]] = std::min(dp[i][j + as[i]], dp[i - 1][j] * 2);
      }
      if(j * as[i] <= total) {
        dp[i][j * as[i]] = std::min(dp[i][j * as[i]], dp[i - 1][j] * 2 + 1);
      }
    }
  }

  long long ans{dp[n - 1][total]};
  // if(ans == nil) throw "okasii";
  std::vector<char> anss;
  for(int i{}; i < n - 1; ++i) {
    if(ans % 2) {
      anss.push_back('*');
    } else {
      anss.push_back('+');
    }
    ans /= 2;
  }

  for(int i{n - 2}; i >= 0; --i) {
    std::cout << anss[i];
  }
  std::cout << std::endl;

  return 0;
}
0