Codeforces Round #735 (Div. 2) C. Mikasa

49 篇文章 0 订阅
24 篇文章 0 订阅

题目大意

给出 n , m n,m n,m求集合 { n ⨁ 0 , n ⨁ 1 , ⋯   , n ⨁ m } \{n\bigoplus0,n\bigoplus1,\cdots,n\bigoplus m\} {n0,n1,,nm}的MEX。

时间限制

1s

数据范围

n , m ≤ 1 0 9 n,m\le10^9 n,m109

题解

对于一个 x x x,如果它存在集合中,即有 n ⨁ y = x , y ≤ m n\bigoplus y = x, y\le m ny=x,ym
则有 n ⨁ x = y n\bigoplus x = y nx=y
显然我们就是要找到最小的 x x x使得 n ⨁ x ≥ m n\bigoplus x\ge m nxm

对于异或,最经典的一个思路就是按位计算,从高位往低位贪心。

Code

//#pragma GCC optimize (2)
//#pragma G++ optimize (2)
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <queue>
#include <map>
#define ll long long
#define G getchar
using namespace std;

int read()
{
    char ch;
    for(ch = G();ch < '0' || ch > '9';ch = G());
    int n = 0;
    for(;'0' <= ch && ch <= '9';ch = G())n = (n<<1)+(n<<3)+ch-48;
    return n;
}

void write(int x)
{
    if (x > 9)
    {
        write(x / 10);
        putchar(x % 10 + 48);
    }
    else putchar(x + 48);
}

const int N = 131092;

int n , m , ans , z[32];

int main()
{
    //freopen("b.in","r",stdin);
    //freopen("e.out","w",stdout);
    
    z[0] = 1;
    for (int i = 1 ; i < 32 ; i++)
        z[i] = z[i - 1] << 1;

    for (int T = read() ; T ; T--)
    {
        n = read();
        m = read() + 1;
        ans = 0;
        for (int i = 31 ; i >= 0 ; i--)
            if (m & z[i])
            {
                if ((n & z[i]) == 0) ans = ans | z[i];
            }
            else
            {
                if (n & z[i]) break;
            }

        printf("%d\n", ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值