博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #647 (Div. 2) - Thanks, Algo Muse!C. Johnny and Another Rating Drop(找规律)---题解
阅读量:4035 次
发布时间:2019-05-24

本文共 2406 字,大约阅读时间需要 8 分钟。

来源:

The last contest held on Johnny’s favorite competitive programming platform has been received rather positively. However, Johnny’s rating has dropped again! He thinks that the presented tasks are lovely, but don’t show the truth about competitors’ skills.

The boy is now looking at the ratings of consecutive participants written in a binary system. He thinks that the more such ratings differ, the more unfair is that such people are next to each other. He defines the difference between two numbers as the number of bit positions, where one number has zero, and another has one (we suppose that numbers are padded with leading zeros to the same length). For example, the difference of 5=1012 and 14=11102 equals to 3, since 0101 and 1110 differ in 3 positions. Johnny defines the unfairness of the contest as the sum of such differences counted for neighboring participants.

Johnny has just sent you the rating sequence and wants you to find the unfairness of the competition. You have noticed that you’ve got a sequence of consecutive integers from 0 to n. That’s strange, but the boy stubbornly says that everything is right. So help him and find the desired unfairness for received numbers.

Input

The input consists of multiple test cases. The first line contains one integer t (1≤t≤10000) — the number of test cases. The following t lines contain a description of test cases.

The first and only line in each test case contains a single integer n (1≤n≤1018).

Output

Output t lines. For each test case, you should output a single line with one integer — the unfairness of the contest if the rating sequence equals to 0, 1, …, n−1, n.

Example

inputCopy
5
5
7
11
1
2000000000000
outputCopy
8
11
19
1
3999999999987
Note
For n=5 we calculate unfairness of the following sequence (numbers from 0 to 5 written in binary with extra leading zeroes, so they all have the same length):

000

001
010
011
100
101
The differences are equal to 1, 2, 1, 3, 1 respectively, so unfairness is equal to 1+2+1+3+1=8.
题意:
给出一个数n,输出0~n相邻两个数间的差异位数的总和

思路:

可以发现规律,对于每一个2^k ,那么最后的答案就是2^k+1 - 1

代码:

#include 
#include
#define maxn 1100using namespace std;#define ll long longint a[maxn], b[maxn];int main() {
int t; cin >> t; ll a,c; while (t--) {
cin >> a; c = 0; for (c = 0; a; a >>= 1) c += a & 1; cout << a * 2 - c << endl; } return 0;}

转载地址:http://bofdi.baihongyu.com/

你可能感兴趣的文章
git 常用命令
查看>>
linux位操作API
查看>>
uboot.lds文件分析
查看>>
uboot start.s文件分析
查看>>
没有路由器的情况下,开发板,虚拟机Ubuntu,win10主机,三者也可以ping通
查看>>
本地服务方式搭建etcd集群
查看>>
安装k8s Master高可用集群
查看>>
忽略图片透明区域的事件(Flex)
查看>>
忽略图片透明区域的事件(Flex)
查看>>
AS3 Flex基础知识100条
查看>>
Flex动态获取flash资源库文件
查看>>
flex中设置Label标签文字的自动换行
查看>>
Flex 中的元数据标签
查看>>
flex4 中创建自定义弹出窗口
查看>>
01Java基础语法-11. 数据类型之间的转换
查看>>
01Java基础语法-13. if分支语句的灵活使用
查看>>
01Java基础语法-15.for循环结构
查看>>
01Java基础语法-16. while循环结构
查看>>
01Java基础语法-17. do..while循环结构
查看>>
01Java基础语法-18. 各种循环语句的区别和应用场景
查看>>