CodeForces-986C.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(nullptr);
  6. int n, m;
  7. cin >> n >> m;
  8. int size = 1 << n;
  9. int full = size - 1;
  10. vector<char> present(size, false), seen_value(size, false), seen_mask(size, false);
  11. vector<int> values(m);
  12. for (int &x : values) {
  13. cin >> x;
  14. present[x] = true;
  15. }
  16. auto explore_component = [&](int source) {
  17. vector<pair<int, int>> stack;
  18. seen_value[source] = true;
  19. stack.push_back({source, 0}); // type 0: real value, type 1: submask state
  20. while (!stack.empty()) {
  21. auto [x, type] = stack.back();
  22. stack.pop_back();
  23. if (type == 0) {
  24. int mask = full ^ x;
  25. if (!seen_mask[mask]) {
  26. seen_mask[mask] = true;
  27. stack.push_back({mask, 1});
  28. }
  29. } else {
  30. if (present[x] && !seen_value[x]) {
  31. seen_value[x] = true;
  32. stack.push_back({x, 0});
  33. }
  34. for (int bits = x; bits != 0; bits &= bits - 1) {
  35. int bit = bits & -bits;
  36. int next = x ^ bit;
  37. if (!seen_mask[next]) {
  38. seen_mask[next] = true;
  39. stack.push_back({next, 1});
  40. }
  41. }
  42. }
  43. }
  44. };
  45. int components = 0;
  46. for (int x : values) {
  47. if (!seen_value[x]) {
  48. ++components;
  49. explore_component(x);
  50. }
  51. }
  52. cout << components << '\n';
  53. return 0;
  54. }