hdu 5747 Aaronson (BestCoder Round #84 1001)

至今不懂官方题解是什么意思答案就是popcount(n)-popcount(\lfloor \frac{n}{2^m} \rfloor) + \lfloor \frac{n}{2^m} \rfloorpopcount(n)popcount(2mn)+2mn.

rfloor

popcount是数出一个二进制数中有几个1,如0110就是2

用贪心过得比较多:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;

ll mypow(ll x,ll n){
    ll res = 1;
    while(n){
        if(1 & n) res = res *x;
        x = x*x;
        n >>= 1;
    }
    return res;
}
ll _2pow[31];
void initial(){
    for(int i=0;i <=30;i++)
        _2pow[i] = mypow(2,i);
}
int main()
{
    initial();
    //freopen("in.txt","r",stdin);
    int n,m,t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        int ans =  0;
        m = m >= 31?30:m;
        for(int i = m;i>=0 && n;i--){
            if( n >= _2pow[i]){
                int xi = n / _2pow[i];
                n = n - (xi *_2pow[i]);
                ans += xi;
            }
        }
        printf("%d\n",ans);

    }
    return 0;
}




/*题解的做法..着实想不到,后来敲了一遍
//自己写的,比较挫..
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;

ll mypow(ll x,ll n){
    ll res = 1;
    while(n){
        if(1 & n) res = res *x;
        x = x*x;
        n >>= 1;
    }
    return res;
}
int popcout(ll num){
    int cnt = 0;
    while (num){
        if(num & 1) cnt++;
        num >>= 1;
    }
    return cnt;
}
int main()
{
    int n,m;
    int t;
    scanf("%d",&t);
    while(t--){

        scanf("%d%d",&n,&m);

        if(m>31)m=30;

        ll A = popcout(n);
        ll B = popcout(n/mypow(2,m));
        ll C = (ll)n/mypow(2,m);

        printf("%I64d\n", A - B + C);
    }
    return 0;
}

*/








popcount(n)-popcount(\lfloor \frac{n}{2^m} \rfloor) + \lfloo
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值