Advertisement
Eternoseeker

AND quest

Nov 24th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef vector<int> vi;
  5.  
  6. void solve() {
  7.     int n;
  8.     ll k;
  9.     cin >> n >> k;
  10.     vector<ll> arr(n);
  11.     for (int i = 0; i < n; i++) cin >> arr[i];
  12.    
  13.     vector<int> indices;
  14.     for (int i = 0; i < n; i++) {
  15.         if ((arr[i] & k) == k) { // Elements that have all k's bits set
  16.             indices.push_back(i + 1);
  17.         }
  18.     }
  19.    
  20.     if (indices.empty()) {
  21.         cout << "NO\n";
  22.         return;
  23.     }
  24.    
  25.     int m = indices.size();
  26.     for (int len = 1; len <= m; len++) {
  27.         ll curr_and = arr[indices[0] - 1];
  28.         bool found = false;
  29.        
  30.         for (int i = 1; i < len; i++) {
  31.             curr_and &= arr[indices[i] - 1];
  32.         }
  33.        
  34.         if (curr_and == k) {
  35.             cout << "YES\n" << len << "\n";
  36.             for (int i = 0; i < len; i++) {
  37.                 cout << indices[i] << (i == len-1 ? "\n" : " ");
  38.             }
  39.             return;
  40.         }
  41.     }
  42.    
  43.     cout << "NO\n";
  44. }
  45.  
  46. int main() {
  47.     ios_base::sync_with_stdio(false);
  48.     cin.tie(nullptr);
  49.     int t;
  50.     cin >> t;
  51.     while (t--) solve();
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement