Discription

Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight  (where  is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph?

You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph

You can read about the minimum spanning tree inhttps://en.wikipedia.org/wiki/Minimum_spanning_tree

The weight of the minimum spanning tree is the sum of the weights on the edges included in it.

Input

The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.

Output

The only line contains an integer x, the weight of the graph's minimum spanning tree.

Example

Input
4
Output
4

Note

In the first sample: The weight of the minimum spanning tree is 1+2+1=4.

依次考虑加入边权 1,2.....的边,看能否使图的连通性产生变化。

发现只有 2^i 的边能对图的连通性产生变化,并且有用的边的数量也很好计算 (不妨画一个图就能很快的发现这个规律),所以就可以直接 递归/迭代 做了。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline ll LB(ll x){ return x&-x;}
ll solve(ll x){
return x==1?0:(solve(x>>1)*2ll+(x>>1)+((x&1)?LB(x-1):0));
}
int main(){
ll n; scanf("%I64d",&n);
printf("%I64d\n",solve(n));
return 0;
}

  

最新文章

  1. Samba快速配置
  2. 2016HUAS_ACM暑假集训4K - 基础DP
  3. hdu.5203.Rikka with wood sticks(数学推导:一条长度为L的线段经分割后可以构成几种三角形)
  4. duapp获取mysql用户名密码等等……
  5. BZOJ3483 : SGU505 Prefixes and suffixes(询问在线版)
  6. 20150410---GridView分页(备忘)
  7. Andorid游戏2048开发(一)
  8. How to Build CyanogenMod for One X (codename: endeavoru)
  9. Oracle数据库之开发PL/SQL子程序和包
  10. ViewPager实现首次进入软件时左右滑屏的软件展示效果
  11. 14. Redis配置统计字典
  12. SAS对数据变量的处理
  13. Codeforces Round #424 E. Cards Sorting
  14. 动态创建 Plist 文件
  15. 个基于TensorFlow的简单故事生成案例:带你了解LSTM
  16. android studio 修改gradle引用本地文件
  17. LDA-MySql
  18. GraphQL介绍&amp;使用nestjs构建GraphQL查询服务
  19. LeetCode 中级 - 重新排序得到的幂(105)
  20. img标签使用onload进行src更改时出现的内存溢出问题

热门文章

  1. 安装pycharm 2018.3 Professional Edition
  2. javase(2)_递归&amp;迭代
  3. Ukulele 那些花儿
  4. LuoguP1351 联合权值 (枚举)
  5. 文件操作-mkdir
  6. ubuntu下如何对接斗鱼直播
  7. node.js中常用的fs文件系统
  8. 我的Python分析成长之路5
  9. Python-约瑟夫环
  10. Python Hashlib笔记