kyopro-lib

This documentation is automatically generated by online-judge-tools/verification-helper

View on GitHub

:warning: Subset sum problem
(Mylib/Typical/subset_sum.cpp)

Operations

Requirements

Notes

Problems

References

Code

#pragma once
#include <cassert>
#include <vector>

namespace haar_lib {
  auto subset_sum(int N, int K, const std::vector<int> &a) {
    assert((int) a.size() == N);
    std::vector<std::vector<bool>> dp(2, std::vector<bool>(K + 1));

    dp[0][0] = true;

    for (int i = 0; i < N; ++i) {
      for (int j = 0; j <= K; ++j) {
        if (j >= a[i])
          dp[(i + 1) & 1][j] = dp[i & 1][j - a[i]] or dp[i & 1][j];
        else
          dp[(i + 1) & 1][j] = dp[i & 1][j];
      }
    }

    return dp[N & 1];
  }
}  // namespace haar_lib
#line 2 "Mylib/Typical/subset_sum.cpp"
#include <cassert>
#include <vector>

namespace haar_lib {
  auto subset_sum(int N, int K, const std::vector<int> &a) {
    assert((int) a.size() == N);
    std::vector<std::vector<bool>> dp(2, std::vector<bool>(K + 1));

    dp[0][0] = true;

    for (int i = 0; i < N; ++i) {
      for (int j = 0; j <= K; ++j) {
        if (j >= a[i])
          dp[(i + 1) & 1][j] = dp[i & 1][j - a[i]] or dp[i & 1][j];
        else
          dp[(i + 1) & 1][j] = dp[i & 1][j];
      }
    }

    return dp[N & 1];
  }
}  // namespace haar_lib
Back to top page