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