Advertisement
sherry_ahmos

Untitled

Nov 30th, 2022 (edited)
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. #define ll long long
  8. #define cy cout << "YES"
  9. #define cn cout << "NO"
  10. #define nl "\n"
  11. #define fi first
  12. #define se second
  13. #define cin(v) for (int i = 0; i < n, cin >> v[i]; i++)
  14. #define cout(v) for (int i = 0; i < v.size(), cout << v[i] << " "; i++)
  15. #define all(v) v.begin(), v.end()
  16. #define sz(s) s.size()
  17.  
  18. void sherry()
  19. {
  20.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  21. #ifndef ONLINE_JUDGE
  22.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  23. #endif
  24. }
  25. int n, k;
  26. vector<int> a;
  27. vector<vector<int>> dp;
  28. int fix(int x)
  29.     return (x % k + k) % k;
  30. bool isdiv(int idx, int sum)
  31. {
  32.     if (idx == n)
  33.         return sum == 0;
  34.     int &ret = dp[idx][sum];
  35.     if (~ret)
  36.         return ret;
  37.     return (ret = isdiv(idx + 1, fix(sum - a[idx])) || isdiv(idx + 1, fix(sum + a[idx])));
  38. }
  39. void solve()
  40. {
  41.     cin >> n >> k;
  42.     a.resize(n);
  43.     cin(a);
  44.     dp.assign(n, vector<int>(k, -1));
  45.     cout << (isdiv(1, fix(a[0]))? "Divisible":"Not divisible")<< nl;
  46. }
  47. int main()
  48. {
  49.     sherry();
  50.     int t = 1;
  51.     cin >> t;
  52.     while (t--)
  53.         solve();
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement