Codeforces 862 C. Mahmoud and Ehab and the xor 位运算

  题目链接: http://codeforces.com/problemset/problem/862/C

  题目描述: 给你n, k 找到一个大小为n的集合, 使得集合异或等于k

  解题思路: 自己一开始想的思路是有一个数必须是x, 剩下的异或为0, 想了好久没有什么思路.......看代码吧, 不复杂的, 主要是利用了异或的性质

  代码: 

#include <iostream>
#include <cstdio>
#include <map>
#include <iterator>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long ll;
const int maxn = 1e5+10;

ll base = 1e6;
int n, x;

int main() {
    scanf("%d%d", &n, &x);
    if( n == 2 && x == 0 ) { printf( "NO
" ); return 0; }
    printf( "YES
" );
    ll ans = 0;
    if( n % 2 == 0 ) {
        printf( "0 " );
        n--;
    }
    for( int i = 0; i < n-1; i++ ) {
        ll temp = base - i;
        if( i == n-2 && ans ^ x ^ temp == 0 ) temp--;
        ans = ans ^ temp;
        printf( "%lld ", temp );
    }
    printf( "%lld
", ans ^ x );
    return 0;
}
View Code

  思考: 自己的位运算也不好, 哎, 真的是弱项实在是太多了, 自己还是需要多加大比赛, 训练自己将问题抽象出来的能力和代码能力, 同时慢慢的扩充自己的知识点, 两天一场CF

原文地址:https://www.cnblogs.com/FriskyPuppy/p/7608593.html