2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 A题

A - ^&^

Bit operation is a common computing method in computer science ,Now we have two positive integers  AA and BB ,Please find a positive integer CC that minimize the value of the formula (A  xor  C)  &  (B  xor  C)(A  xor  C)  &  (B  xor  C) .Sometimes we can find a lot of CC to do this ,So you need to find the smallest CC that meets the criteria . 

For example ,Let's say AA is equal to 5 and BB is equal to 3 ,we can choose CC=1,3.... ,so the answer we're looking for CC is equal to 1. 

If the value of the expression is 0 when C=0, please print 1. 

Input

The input file contains TT test samples.(1<=TT<=100) 

The first line of input file is an integer TT. 

Then the TT lines contains 2 positive integers, AA and BB, (1A,B<2321≤A,B<232)
OutputFor each test case,you should output the answer and a line for each answer. 
Sample Input

1
3 5

Sample Output

1


   由题意:找到最小C,使(A xor C)&(B xor C)最小。根据分配律,原式=(A&B) xor C;
   A,B已知,则(A&B)为固定值,又根据xor(异或)知识:同为0,异为1,则要想使原式最小,C要==(A&B)才行,因为两个数异或运算,如果两个数相同,同为0,结果最小。
所以C=(A&B)。
  又根据题意最后一句:If the value of the expression is 0 when C=0, please print 1. 
    有代码:
    
#include<iostream>
typedef long long ll;
using namespace std;
int main()
{
    ll t;
    cin>>t;
    while(t--)
    {
        ll a,b;
        cin>>a>>b;
        if((a&b)==0)
            printf("1\n");
        else
            printf("%lld\n",a&b);
    }
}

 

转载于:https://www.cnblogs.com/liyexin/p/11428091.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值