BestCoder Round #84 Aaronson

Aaronson

题意:

给个中文链接:戳戳戳

题解:

这题一看给的公式就是二进制,之后马上就能想到当m大于等于二进制的位数时,输出n的二进制的1的个数就好了。之后就是m小于二进制的位数时,只要加上2的(总位数-m)次方就好了,当且仅当这一位是1的时候成立。

代码:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

const int INF=0x3f3f3f3f;
typedef long long ll;
#define PU puts("");
#define PI(A) printf("%d\n",A)
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
const double EPS= 1e-9 ;

/*  /     C o d i n g  S p a c e     /  */

const int MAXN= 1000 + 9 ;

int main()
{
    int o;
    SI(o);
    while(o--)
    {
        int a,b;
        SII(a,b);
        int c=a;
        int cnt=-1,cnt1=0;
        while(c)
        {
            if (c&1) cnt1++;
            cnt++;
            c>>=1;
        }
        if (cnt<=b) PI(cnt1);
        else
        {
            c=a;
            int ct=0,ct1=0;
            int ans=0;
            while(c)
            {
                if (c&1)
                {
                    if (ct<=b)
                    {
                        ans++;
                    }
                    else
                    {
                        ans=ans+(int)pow(2.0,ct-b);
                    }
                }
                ct++;
                c>>=1;
            }
            PI(ans);
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/s1124yy/p/5700149.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值