[Codeforces]862C. Mahmoud and Ehab and the xor

題目連結:http://codeforces.com/contest/862/problem/C

需要構造一個長度為n的序列,其全部xor起來等於k。
可以發現0^1^2^3為0 ,4^5^6^7為0,8^9^10^11為0........。
所以先構造出n%4個數字其xor總和為k,接著再如上所寫的規律一組一組丟進ans。
接著就特判一些東西,會無解的可能情況只有n = 2和k = 0的時候,同理當(n%4 = 2,k = 0)時因為無法構造出2個數字xor = 0,所以先隨便丟一些數字進去再構造即可。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <queue>
#include <vector>
#define lld long long
#define PB push_back
#define F first
#define S second
#define jizz cin.tie(0);ios_base::sync_with_stdio(0);
#define endl '\n'
#define N 1000000
using namespace std;
typedef pair<int,int> Pair;
vector<int> ans;
bitset<1000006> ok;
int tmp = (1 << 18)-1,t = 0;
int main(){jizz
    int n,k;cin >> n >> k;
    bool flag = 0;
    if(n == 2 && !k)return cout << "NO" << endl, 0;
    if(n%4 == 2 && !k){
        flag = 1;
        ans.PB(1);ans.PB(2);ans.PB(3);
        ok[1] = ok[2] = ok[3] = 1;
        n -= 3;
    }
    for(int i = 0 ; i < (n-1)%4 ; i++){
        while(k == tmp-t || ok[tmp-t] || ok[k^(tmp-t)])t++;
        k ^= (tmp-t);
        ok[tmp-t] = 1;
        ans.PB(tmp-t);
    }ans.PB(k),ok[k] = 1;
    if(flag)n += 3;
    for(int i = 0 ; ans.size() < n ; i+=4){
        if(ok[i] || ok[i+1] || ok[i+2] || ok[i+3])continue;
        ans.PB(i),ans.PB(i+1),ans.PB(i+2),ans.PB(i+3);
    }
    cout << "YES" << endl;
    for(int i : ans)cout << i << ' ';
    return 0;
}




留言

這個網誌中的熱門文章

[TIOJ]1617. [Interactive] 中位數

[TIOJ]1337. 隕石

[TIOJ]1994. 冰塊線