【CF-1362】C. Johnny and Another Rating Drop

C. Johnny and Another Rating Drop

题意

定义两个数字的差异为他们二进制相应位置不一样的个数,给出n,让求 0 和 1 , 1 和 2 ... n-1 和 n 的差异和。

思路

n这么大,多半是有规律的。

打表发现

1 1

2 3

4 7

8 15

把 n 表示为二进制,如果它的第 i 位为 1 ,那么最终的答案 + \(2^{i+1}-1\)

代码

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N=1024+10;
const int mod=50331653;
const int inf=0x3f3f3f3f;
typedef unsigned long long ull;
typedef long long ll;

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ull n;
        scanf("%llu",&n);
        ull x=1,ans=0;
        for(int i=0;i<64;i++)
        {
            if((x<<i)&n)
                ans+=(x<<(i+1))-1;
        }
        printf("%llu\n",ans);
    }
    return 0;
}
/*
*/

posted @ 2020-06-08 18:28  Valk3  阅读(97)  评论(0编辑  收藏  举报