fork download
  1. #include <bits/stdc++.h>
  2. #include <chrono>
  3. #define int long long
  4. #define all(x) x.begin(), x.end()
  5. #define f1(i, n) for(int i=1;i<=n;++i)
  6. using namespace std;
  7. using namespace chrono;
  8.  
  9. const int maxn = 3e5 + 5;
  10. const int MOD = 1e9 + 7;
  11.  
  12. int A[maxn], cnt[5];
  13.  
  14. main() {
  15. ios::sync_with_stdio(false);
  16. cin.tie(nullptr);
  17. cout.tie(nullptr);
  18.  
  19. int q;
  20. cin >> q;
  21. while (q--) {
  22. int n, k;
  23. cin >> n >> k;
  24. cnt[0] = cnt[1] = cnt[2] = 0;
  25. f1(i, n) {cin >> A[i]; cnt[A[i] % 3]++;}
  26. bool ok = false;
  27. // Duyệt số lượng chọn từ mỗi nhóm mod 3 (vì chỉ có 3 nhóm)
  28. for (int x = 0; x <= min(cnt[0], k); ++x) { // chọn x phần tử mod 0
  29. for (int y = 0; y <= min(cnt[1], k - x); ++y) { // chọn y phần tử mod 1
  30. int z = k - x - y;
  31. if (z < 0 || z > cnt[2]) continue;
  32. if ((y + 2 * z) % 3 == 0) ok = true;
  33. }
  34. }
  35.  
  36. if (ok == true) cout << "YES" << "\n";
  37. else cout << "NO" << "\n";
  38. }
  39.  
  40.  
  41.  
  42. }
  43.  
  44.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
NO