Codeforces #563 (Div. 2) D. Ehab and the Expected XOR Problem (位運算)

鏈接: 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;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章