結果

問題 No.10 +か×か
ユーザー yuppe19 😺yuppe19 😺
提出日時 2017-06-25 16:40:08
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 810 bytes
コンパイル時間 435 ms
コンパイル使用メモリ 52,336 KB
最終ジャッジ日時 2024-04-27 02:26:59
合計ジャッジ時間 747 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:9:3: error: ‘vector’ was not declared in this scope
    9 |   vector<int> a(n); // a[n]
      |   ^~~~~~
main.cpp:3:1: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’?
    2 | #include <algorithm>
  +++ |+#include <vector>
    3 | using namespace std;
main.cpp:9:10: error: expected primary-expression before ‘int’
    9 |   vector<int> a(n); // a[n]
      |          ^~~
main.cpp:10:41: error: ‘a’ was not declared in this scope
   10 |   for(int i=0; i<n; ++i) { scanf("%d", &a[i]); }
      |                                         ^
main.cpp:11:13: error: expected primary-expression before ‘>’ token
   11 |   vector<i64> dp(total+1, -1); // dp[total+1]
      |             ^
main.cpp:11:15: error: ‘dp’ was not declared in this scope
   11 |   vector<i64> dp(total+1, -1); // dp[total+1]
      |               ^~
main.cpp:14:15: error: expected primary-expression before ‘>’ token
   14 |     vector<i64> ndp(total+1, -1); // ndp[total+1]
      |               ^
main.cpp:14:17: error: ‘ndp’ was not declared in this scope
   14 |     vector<i64> ndp(total+1, -1); // ndp[total+1]
      |                 ^~~
main.cpp:17:14: error: ‘a’ was not declared in this scope
   17 |       if(j + a[i] <= total) { ndp[j+a[i]] = max(ndp[j+a[i]], (dp[j] << 1) + 1); }
      |              ^
main.cpp:18:14: error: ‘a’ was not declared in this scope
   18 |       if(j * a[i] <= total) { ndp[j*a[i]] = max(ndp[j*a[i]],  dp[j] << 1); }
      |              ^
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);

ソースコード

diff #

#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