結果

問題 No.10 +か×か
コンテスト
ユーザー yuppe19 😺
提出日時 2017-06-25 16:40:08
言語 C++11(old_compat)
(gcc 12.4.0 + boost 1.89.0)
コンパイル:
g++-12 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -include bits/stdc++.h -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 20 ms / 5,000 ms
コード長 810 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,409 ms
コンパイル使用メモリ 173,124 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2026-03-08 16:12:10
合計ジャッジ時間 2,113 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 13
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:7:15: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    7 |   int n; scanf("%d", &n);
      |          ~~~~~^~~~~~~~~~
main.cpp:8:19: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    8 |   int total; scanf("%d", &total);
      |              ~~~~~^~~~~~~~~~~~~~
main.cpp:10:33: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   10 |   for(int i=0; i<n; ++i) { scanf("%d", &a[i]); }
      |                            ~~~~~^~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <iostream>
#include <algorithm>
using namespace std;
using i64 = long long;

int main(void) {
  int n; scanf("%d", &n);
  int total; scanf("%d", &total);
  vector<int> a(n); // a[n]
  for(int i=0; i<n; ++i) { scanf("%d", &a[i]); }
  vector<i64> dp(total+1, -1); // dp[total+1]
  dp[0] = 0;
  for(int i=0; i<n; ++i) {
    vector<i64> ndp(total+1, -1); // ndp[total+1]
    for(int j=0; j<=total; ++j) {
      if(dp[j] == -1) { continue; }
      if(j + a[i] <= total) { ndp[j+a[i]] = max(ndp[j+a[i]], (dp[j] << 1) + 1); }
      if(j * a[i] <= total) { ndp[j*a[i]] = max(ndp[j*a[i]],  dp[j] << 1); }
    }
    dp = ndp;
  }
  string res = "";
  for(i64 val=dp[total]; val; val>>=1) {
    res += "*+"[val & 1];
  }
  reverse(begin(res), end(res));
  res = res.substr(1);
  puts(res.c_str());
  return 0;
}
0