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. const int N = 1e2 + 5;
  12. const int S = 1e2 + 5;
  13. const int LEN = 1e2 + 5;
  14. const int MOD = 998244353;
  15.  
  16. void add(int& a, int b) {
  17. a += b;
  18. if (a >= MOD) a -= MOD;
  19. }
  20.  
  21. // Ở bài này ta sẽ for để cố định trước len = Số phần tử sẽ được chọn rồi sau đó mới đi tính dp với mỗi len
  22. int n;
  23. int a[N];
  24. int dp[N][LEN][S]; // dp[i][j][sum_mod] = Số tập con có j phần tử khi xét đến vị trí i
  25. // và có tổng mod len = sum_mod
  26.  
  27. int main() {
  28. ios::sync_with_stdio(false);
  29. cin.tie(nullptr);
  30. cin >> n;
  31. for (int i = 1; i <= n; i++) cin >> a[i];
  32.  
  33. int ans = 0;
  34. for (int len = 1; len <= n; len++) {
  35. memset(dp, 0, sizeof dp);
  36. dp[0][0][0] = 1;
  37.  
  38. for (int i = 1; i <= n; i++) {
  39. for (int j = 0; j <= len; j++) {
  40. for (int pre_sum_mod = 0; pre_sum_mod < len; pre_sum_mod++) {
  41. // Không chọn a[i]
  42. add(dp[i][j][pre_sum_mod], dp[i - 1][j][pre_sum_mod]);
  43. // Chọn a[i]
  44. if (j > 0) add(dp[i][j][(pre_sum_mod + a[i]) % len], dp[i - 1][j - 1][pre_sum_mod]);
  45. }
  46. }
  47. }
  48.  
  49. add(ans, dp[n][len][0]);
  50. } // O(n^4)
  51.  
  52. cout << ans << '\n';
  53. }
Success #stdin #stdout 0.01s 8064KB
stdin
3
2 6 2
stdout
6