Advertisement
milon34

subset sum divisible by m

Mar 5th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. problem link:https://codeforces.com/contest/577/problem/B
  2.  
  3.  
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7. #define ll long long int
  8.  
  9. #include <ext/pb_ds/assoc_container.hpp>
  10.  
  11. using namespace __gnu_pbds;
  12. typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  13. const int mx = 1e3 + 5;
  14. int n, m;
  15. int dp[mx][mx][2];
  16. int arr[mx];
  17.  
  18. int solve(int i, int sum, int flag) {
  19.     if (i >= n) {
  20.         return sum == 0 && flag;
  21.     }
  22.     if (dp[i][sum][flag] != -1)return dp[i][sum][flag];
  23.     int r1 = solve(i + 1, sum, flag);
  24.     int r2 = solve(i + 1, (sum + arr[i]) % m, 1);
  25.     return dp[i][sum][flag] = r1 | r2;
  26. }
  27.  
  28. int main() {
  29.     ios_base::sync_with_stdio(false);
  30.     cin.tie(0);
  31.     cin >> n >> m;
  32.     memset(dp, -1, sizeof(dp));
  33.     for (int i = 0; i < n; ++i) {
  34.         cin >> arr[i];
  35.         arr[i] %= m;
  36.     }
  37.     if (n >= m) {
  38.         cout << "YES" << "\n";
  39.         return 0;
  40.     }
  41.     int res = solve(0, 0, 0);
  42.     if (res) {
  43.         cout << "YES" << "\n";
  44.     } else {
  45.         cout << "NO" << "\n";
  46.     }
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement