CF-1362C. Johnny and Another Rating Drop

Posted valk3

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF-1362C. 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
",ans);
    }
    return 0;
}
/*
*/

以上是关于CF-1362C. Johnny and Another Rating Drop的主要内容,如果未能解决你的问题,请参考以下文章

CF-1362B. Johnny and His Hobbies

CF-1362B. Johnny and His Hobbies

CF-1362D. Johnny and Contribution

CF-1362D. Johnny and Contribution

题解 CF1361B Johnny and Grandmaster

Johnny and Grandmaster