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;
}
Codeforces 959 E Mahmoud and Ehab and the xor-MST的更多相关文章
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 959 D Mahmoud and Ehab and another array construction task
Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of ...
- codeforces 862 C. Mahmoud and Ehab and the xor(构造)
题目链接:http://codeforces.com/contest/862/problem/C 题解:一道简单的构造题,一般构造题差不多都考自己脑补,脑洞一开就过了 由于数据x只有1e5,但是要求是 ...
- Codeforces 862C - Mahmoud and Ehab and the xor
862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...
- Coderfroces 862 C. Mahmoud and Ehab and the xor
C. Mahmoud and Ehab and the xor Mahmoud and Ehab are on the third stage of their adventures now. As ...
- CF 959 E. Mahmoud and Ehab and the xor-MST
E. Mahmoud and Ehab and the xor-MST https://codeforces.com/contest/959/problem/E 分析: 每个点x应该和x ^ lowb ...
- codeforces 862B B. Mahmoud and Ehab and the bipartiteness
http://codeforces.com/problemset/problem/862/B 题意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且 ...
- CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)
<题目链接> 题目大意: 给定一个数n,代表有一个0~n-1的完全图,该图中所有边的边权为两端点的异或值,求这个图的MST的值. 解题分析: 数据较大,$10^{12}$个点的完全图,然后 ...
- CodeForces - 862C Mahmoud and Ehab and the xor(构造)【异或】
<题目链接> 题目大意: 给出n.m,现在需要你输出任意n个不相同的数(n,m<1e5),使他们的异或结果为m,如果不存在n个不相同的数异或结果为m,则输出"NO" ...
随机推荐
- syslog(),closelog()与openlog()--日志操作函数 (2)
文章出处:http://blog.chinaunix.net/uid-26583794-id-3166083.html 守护进程日志的实现 syslogd守护进程用于解决守护进程的日志记录问题,而日志 ...
- PAT (Basic Level) Practise (中文)-1037. 在霍格沃茨找零钱(20)
PAT (Basic Level) Practise (中文)-1037. 在霍格沃茨找零钱(20) http://www.patest.cn/contests/pat-b-practise/1037 ...
- 二. python函数与模块
第四章.内置函数与装饰器详解 1.内置函数补充1 注:红色圆圈:必会: 紫红色方框:熟练: 绿色:了解 callable() 判断函数是否可以被调用执行 def f1(): pass f1() ...
- 洛谷 P1483 序列变换
https://www.luogu.org/problemnew/show/P1483 数据范围不是太大. 一个数组记录给k,记录每个数加了多少. 对于查询每个数的大小,那么就枚举每个数的因子,加上这 ...
- 使用Spring AOP实现业务依赖解耦
Spring IOC用于解决对象依赖之间的解耦,而Spring AOP则用于解决业务依赖之间的解耦: 统一在一个地方定义[通用功能],通过声明的方式定义这些通用的功能以何种[方式][织入]到某些[特定 ...
- H5新人福音~零配置搭建现代化的前端工程
X-BUILD一套基于Webpack(v4.21.0)快速搭建H5场景开发环境的脚手架,只需要几分钟的时间就可以运行起来.X-BUILD是针对H5开发的一套自动化构建工具,致力于提升开发效率,减小开发 ...
- 【HDU 2126】Buy the souvenirs(01背包)
When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souve ...
- hdu 5878
I Count Two Three Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- .NET:权限管理
题外话: 临近大四,编写各种简历的时候发现,很多电子简历上是可以链上自己在各大论坛上留下的足迹.关于这点,学习网络,拥抱开源,具有互联网思维的博主很后悔,后悔当年只会在网上查资料,不会留资料,空有才能 ...
- SQLite Database Browser 2.0使用方法
在网上找一个SQLITE查看器 这个查看器叫做:www.jb51.net/database/118822.html 这个查看器可以新建SQLITE文件数据库,可以建立表索引,写SQL语句,编辑表数据 ...