8.23CCPC网络赛——(HDU6702)^&^

3 篇文章 0 订阅
2 篇文章 0 订阅

题目

Bit operation is a common computing method in computer science ,Now we have two positive integers A and B, Please find a positive integer C that minimize the value of the formula (A xor C) & (B xor C) .Sometimes we can find a lot of C to do this ,So you need to find the smallest C that meets the criteria . For example ,Let’s say A is equal to 5 and B is equal to 3 ,we can choose C=1,3… ,so the answer we’re looking for C is equal to 1. If the value of the expression is 0 when C=0, please print 1.

题意

寻找能使表达式 (A xor C) & (B xor C) 值最小的,且自身值也最小的C。特别的,当表达式的值和C的最小值都是0的时候,就直接输出1。(其中,XOR就是^或运算,在二进制中,两个对应的数位上,不同为1,相同为0;&是 与运算,在二进制中,两个对应的数位上,同1出1,有0出0。)

解析(本题是思维题)

错误思路:之前比赛的时候啥也不知道,以为是模拟题,然后就试图用循环找表达式=0的C,结果不论输入啥出来的都是1。那个特判的C=0也没有理解到位,以为C是从1开始找的,因为题目给的例子就是从1开始的,然后就一直很懵。
正确思路:(1)因为任何数异或0都是它本身,C最小值可以是0,要是A&B=0,那么根据题目特判条件(第二种理解思路见2),直接输出1。(2)重新解释一下题目的样例:由“A=5,二进制中为0101;B=3,二进制中为0011;当C=1,二进制中为0001时,表达式有最小值0000”可得,只需要在A和B都是1的位上C也对应是1(即A&B,&运算法则同1出1,有0出0)就行。当A&B=0时,C=0,但是C要是正整数,所以C=1。这样就可以解释为什么题目中的解释C可以有很多比如1,3(这个是除了对应位是1,其他位不限),…,最小值是1。

代码

错误代码(WA)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
#define ll unsigned long long
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll a,b,c,f;
        scanf("%lld%lld",&a,&b);
        for(ll i=0; i<100; i++)
        {
            ll j=i+1;
            ll x1=(a&i)&(b^i);
            ll x2=(a^j)&(b^j);
            if(x1<x2) f=x1,c=i;
            else f=x2,c=j;
            if(f==0) break;
        }
        if(f==0) printf("1\n");
        else printf("%lld\n",c);
    }
    return 0;
}

正确代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
#define ll long long
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        ll a,b;
        scanf("%lld%lld",&a,&b);
        if(((a^0)&(b^0))==0) printf("1\n");
        else printf("%lld\n",a&b);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值