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;
}

 

 

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