Codeforces635C XOR Equation【数学】

题目链接:

http://codeforces.com/contest/635/problem/C

题意:

给定两个数的和s及异或x,求两个数的可能情况。

分析:

我们有公式 a+b=a & b2+a ^ b
这样对于与和异或的结果一位一位的来考虑即可。
注意:

  1. 题目特别强调Two positive integers a and b,所以在s x <script type="math/tex" id="MathJax-Element-518">x</script>相等时,我们要减去0的情况。

    • 差为奇数的情况很明显不存在ab。
    • 按位判断的时候注意xx和tt都为1的情况也是不存在的。
    • 代码:

      #include<cstdio>
      #include<iostream>
      #include<cstring>
      using namespace std;
      #define pr(x) cout << #x << ": " << x << "  "
      #define pl(x) cout << #x << ": " << x << endl;
      #define sa(x) scanf("%d",&(x))
      #define sal(x) scanf("%I64d",&(x))
      #define mdzz cout<<"mdzz"<<endl;
      const int maxn = 2e5 + 5, oo =0x3f3f3f3f;
      typedef long long ll;
      //a + b = a & b * 2 + a ^ b
      int main (void)
      {
          ll s, x;sal(s);sal(x);
          ll t = s - x;
          if(t & 1) return puts("0"),0;
          t >>= 1;
          int tt = 1, xx = 1;
          ll ans = 1;
          ll tx = x;
          while(t || x){
              tt = t & 1;
              xx = x & 1;
              if(!tt && xx) ans <<= 1;
              if(tt && xx) return puts("0"), 0;
              t >>= 1;
              x >>= 1;
          }
          if(s == tx) ans -= 2;
          printf("%I64d", ans);
          return 0;
      }
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值