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 }
相關文章
相關標籤/搜索