鏈接: http://codeforces.com/contest/1174/problem/D
總結:
我是一隻小菜雞,嘰嘰嘰。 建議做個位運算專題叭。
思路:
位運算 思考題
設b[]爲前綴異位和數組。首先我們知道: a[l] ^ a[l+1] ^......^ a[r] = b[r] ^ b[l-1] 。
相鄰al,al+1不能相等,相等異或爲0不符合條件,所以每次i++;查找是否有b[ ]與 i 異或等於x。若沒有將 i 存入set。
最後 a[ i ] = b[ i ] ^ b[ i-1 ] 。
代碼:
#include <cstdio>
#include <map>
#include <iostream>
#include <map>
#include <set>
using namespace std;
int b[4000005];
int main()
{
int n, x;
while(cin>>n)
{
cin>>x;
int i,ans = 0;
set <int> se;
se.insert(0);
for(i = 1;i < (1<<n);i++)
{
if(!se.count(i^x))
{
b[++ans] = i;
se.insert(i);
}
}
printf("%d\n",ans);
for(i = 0;i < ans;i++)
printf("%d ", b[i]^b[i+1]);
cout<<endl;
}
return 0;
}