CF1174D Ehab and the Expected XOR Problem

思路:c++

使用前缀和技巧进行问题转化:原数组的任意子串的异或值不能等于0或x,能够转化成前缀异或数组的任意两个元素的异或值不能等于0或x。数组

实现:spa

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int vis[1 << 18];
 4 int main()
 5 {
 6     int n, x;
 7     while (cin >> n >> x)
 8     {
 9         memset(vis, 0, sizeof vis);
10         vector<int> v;
11         if (x >= (1 << n)) 
12         {
13             for (int i = 1; i < (1 << n); i++) v.push_back(i);
14         }
15         else
16         {
17             for (int i = 1; i < (1 << n); i++)
18             {
19                 if (i == x) continue;
20                 else if (!vis[i]) { v.push_back(i); vis[i ^ x] = 1; }
21             }
22         }
23         cout << v.size() << endl;
24         if (!v.empty())
25         {
26             cout << v[0] << " ";
27             for (int i = 1; i < (int)v.size(); i++) cout << (v[i - 1] ^ v[i]) << " ";
28             cout << endl;            
29         }
30     }
31     return 0;
32 }
相关文章
相关标签/搜索