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" ...
随机推荐
- 获得Java中System对应一些属性值
public static void main(String[] args){ System.out.println("Java运行时环境版本:\n"+System.getProp ...
- nginx在windows上面的启动bat文件
因为windows上面zip安装nginx后启动比较麻烦,然后找了一下关于批处理文件的资料,写了一个nginx启动和关闭的脚本. 这个脚本正常情况下是可以使用的.因为脚本中并没有对nginx程序是否在 ...
- 【Office_Word】Word排版
文档排版的步骤: step1.先设置正文的样式 step2.再设置各级标题的样式 step3.最后在"多级列表"里设置各级标题编号 [注]最好按照这三步的顺序来排版,否则将会导致正 ...
- Django 千锋培训的学习笔记(2)
Django 千锋培训读书笔记 https://www.bilibili.com/video/av17879644/?p=1 切换到创建项目的目录 cd C:\Users\admin\Desktop\ ...
- 牛客网暑期ACM多校训练营(第四场)G Maximum Mode(思维)
链接: https://www.nowcoder.com/login?callBack=%2Facm%2Fcontest%2F142%2FG 题意: 给定n个数, 要求删去恰好m个数后的最大总数是多少 ...
- MFC中关于子进程创建和关闭操作
创建子进程 PROCESS_INFORMATION ProcessInfo; STARTUPINFO StartupInfo; //This is an [in] parameter ZeroMemo ...
- WEB框架——WEB框架本质
武sir http://www.cnblogs.com/wupeiqi/articles/5237672.html
- UIDatePicker 显示时间和打印时间不一样
默认是时区为0,如下格式化一下就好啦 UIDatePicker *datePiker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 100, ...
- 【php学习之路】微信公众帐号
上个月开始学习php,算上中间放假的时间差不多有一个月了.这段时间呢,每天上班没事的时候不慌不忙的看下php,刚开始也只是看下php中文手册,看完一遍好像没什么感觉.于是就想啊,做个什么东 ...
- 基于注解实现SpringBoot多数据源配置
1.功能介绍 在实际的开发中,同一个项目中使用多个数据源是很常见的场景.最近在学习的过程中使用注解的方式实现了一个Springboot项目多数据源的功能.具体实现方式如下. 2.在applicatio ...