classSetAndSet { private: int n; std::vector<int> f; std::vector<int> all[20]; LL res; intfind(int x){ return x == f[x] ? x : f[x] = find(f[x]); } boolmerge(int u, int v) { if (find(u) == find(v)) returnfalse; return f[find(v)] = find(u), true; } voiddfs(int bit, int op) { if (bit == 20) { int cnt = 0; for (int i = 0; i < n; ++ i) cnt += f[i] == i; res += op * ((1LL << cnt) - 2); return; } dfs(bit + 1, op); if (all[bit].size()) { auto bac = f; for (int i = 1; i < (int) all[bit].size(); ++ i) merge(all[bit][i], all[bit][0]); dfs(bit + 1, -op); f = bac; } } public: LL countandset(std::vector<int> a) { n = a.size(), res = 0; f.resize(n); for (int i = 0; i < n; ++ i) f[i] = i; for (int bit = 0; bit < 20; ++ bit) for (int i = 0; i < n; ++ i) if (!(a[i] >> bit & 1)) all[bit].push_back(i); returndfs(0, 1), res; } };