0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

部分和数え上げ問題

Last updated at Posted at 2022-03-15

問題

dp[i][j]
i 番目までの整数の中から(選ぶ、選ばない)
総和を j とするパターン数

const int MOD = 1000000009;

// 入力
int n, A; 
int a[110];

// DPテーブル
int dp[110][10010]; //i番目、総和

int main() {
  cin >> n >> A; //個数、総和
  for (int i = 0; i < n; ++i) cin >> a[i]; //整数

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

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j <= A; ++j) {
      (dp[i+1][j] += dp[i][j]) %= MOD; //a[i]を加算する場合
      if (j >= a[i]) (dp[i+1][j] += dp[i][j-a[i]]) %= MOD; //a[i]を加算しない場合
    }
  }

  cout << dp[n][A] << endl; //n個以下、A以下のパターン数
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?