xay loves or

Description xay loves or. He gives you x and s, you need to calculate how many positive integer y satisfy x or y = s. Input The first line contains two positive integers x and s . It’s guaranteed that 1 ≤ x, s < 2 31 . Output Print the number of y satisfy x or y = s .

Example

standard input

2 5

standard output

 0

 题意:
给出x和s求y

公式 x|y=s

 就是先把s和x转化为二进制

然后再一一比较

比如

x          |            y          ->         s

1                     无                      0

1                   0,1                    1

0                        1                     1

0                        0                      0

当x==1 s==1时

有两个情况

#include <iostream>
#include <cstring>
#include <algorithm>
#include<cmath>
using namespace std;
 
int x,s;
 
int main()
{
    cin>>x>>s;
    int tem=1,flag=0;
    if(x>s) flag=1;
    else
    {
        int k=log(s)/log(2);
        for(int i=k;i>=0;i--)
        {
            int a=(s>>i&1);
            int b=(x>>i&1);
            if(a==1&&b==1)  tem*=2;
            else if(a==0&&b==1) flag=1;
        }
    }
    if(x==s) tem-=1;
    if(flag) cout<<0<<endl;
    else cout<<tem<<endl;
    return 0;
}

看到有人直接暴力过了

太神奇了

 

#include<iostream>

using namespace std;

int main()
{
    int x, s, ans = 0;

    cin>>x>>s;
    for(int i=1;i<=s;i++)
    {
        if((x|i)==s)    ans++;
    }
    cout<<ans<<endl;

    return 0;
}

2^31次方都2e9了不知道为啥能过 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值