fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef pair<int, int> ii;
  6.  
  7. template<typename T>
  8. bool maximize(T& a, const T& b) {
  9. if (b < a) return false;
  10. a = b;
  11. return true;
  12. }
  13.  
  14. const int INF = 1e9;
  15. const ll LINF = 1e18;
  16.  
  17. // dp knapsack
  18. // 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
  19. // ví dụ 2 2 5 khác với 2 5 2
  20.  
  21. const int N = 1e2 + 5;
  22. const int S = 1e6 + 5;
  23. const int MOD = 1e9 + 7;
  24.  
  25. int n, s;
  26. int w[N];
  27. int dp[S];
  28.  
  29. int main() {
  30. ios::sync_with_stdio(0); cin.tie(0);
  31. cin >> n >> s;
  32. for (int i = 1; i <= n; i++) {
  33. cin >> w[i];
  34. }
  35.  
  36. dp[0] = 1;
  37. // O(n * S)
  38. for (int j = 0; j <= s; j++) {
  39. for (int i = 1; i <= n; i++) {
  40. // chọn món vật thứ i
  41. if (j - w[i] >= 0) {
  42. (dp[j] += dp[j - w[i]]) %= MOD;
  43. }
  44. }
  45. }
  46.  
  47. cout << dp[s] << '\n';
  48. }
Success #stdin #stdout 0.01s 5304KB
stdin
3 9
2 3 5
stdout
8