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

本文提供了一种解决CodeForces 862C问题的方法,通过巧妙利用异或运算的特性,构造出一个由n个元素组成的集合,确保该集合的异或结果等于指定值k。文章包含完整的解题思路及代码实现。

  题目链接: 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\n" ); return 0; }
    printf( "YES\n" );
    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\n", ans ^ x );
    return 0;
}
View Code

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

转载于:https://www.cnblogs.com/FriskyPuppy/p/7608593.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值