fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7.  
  8. const int INF = 1e9;
  9. const ll LINF = 1e18;
  10.  
  11. // dp knapsack
  12. // Biến thể: mỗi món vật có thể được chọn nhiều lần và thứ tự chọn có quan trọng
  13. // ví dụ 2 2 5 khác với 2 5 2
  14.  
  15. const int N = 1e2 + 5;
  16. const int S = 1e6 + 5;
  17. const int MOD = 1e9 + 7;
  18.  
  19. int n, s;
  20. int w[N];
  21. int dp[S]; // dp[j] = Số cách tạo tổng j
  22.  
  23. int main() {
  24. ios::sync_with_stdio(false);
  25. cin.tie(nullptr);
  26. cin >> n >> s;
  27. for (int i = 1; i <= n; i++) cin >> w[i];
  28.  
  29. dp[0] = 1;
  30. for (int j = 0; j <= s; j++) {
  31. for (int i = 1; i <= n; i++) {
  32. if (j - w[i] >= 0) {
  33. (dp[j] += dp[j - w[i]]) %= MOD;
  34. }
  35. }
  36. } // O(s * n)
  37.  
  38. cout << dp[s] << '\n';
  39. }
Success #stdin #stdout 0.01s 5308KB
stdin
3 9
2 3 5
stdout
8