题目

\(n\)个点的完全图标号为\([0,n-1]\),\(i\)和\(j\)连边权值为\(i\: xor\:j\),求MST的值


分析

考虑MST有两种解法一种是Prim一种是Kruskal,Prim应该更好理解。

既然是一个完全图,那么每次新加入一个点,只要找到与它连边的边权最小就可以了

所以题目就转换成每次找到一个\(j<i\),最小化\(i\: xor \: j\)

那尽量让\(j\)的高位与\(i\)的高位一致,只改变最后一位即可,

由于异或的运算相同即零不同即一,既然要让\(j<i\),那么让\(j=i\: xor \: lowbit(i)\)才是最优的,

因为若边权\(<lowbit(i)\),那么\(j>i\),若边权\(>lowbit(i)\),为何不令边权\(=lowbit(i)\)呢

所以题目就转换成\(\sum_{i=1}^{n-1} lowbit(i)\)

这个\(O(\log_2n)\)求就可以了

考虑哪些数对\(2^k\)有贡献,那就是\(2^k\)的倍数且非\(2^{k+1}\)的倍数即可,那就是

\[\lfloor\frac{n}{2^k}\rfloor-\lfloor\frac{n}{2^{k+1}}\rfloor=\lceil\frac{n}{2^{k+1}}\rceil
\]

代码

#include <cstdio>
#define rr register
using namespace std;
typedef long long lll; lll ans,n;
signed main(){
scanf("%lld",&n); --n;
for (rr lll two=1;n;n>>=1)
ans+=((n+1)>>1)*two,two<<=1;
return !printf("%lld",ans);
}

#位运算#CF959E Mahmoud and Ehab and the xor-MST的更多相关文章

  1. Codeforces 862C - Mahmoud and Ehab and the xor

    862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...

  2. 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 ...

  3. CF959E Mahmoud and Ehab and the xor-MST 思维

    Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem t ...

  4. [CF959E]Mahmoud and Ehab and the xor-MST题解

    解法 又是一道结论题? 我的做法比较奇怪且没有证明 #include <cstdio> #include <cmath> #define ll long long int ma ...

  5. 【构造】【分类讨论】Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor

    题意:给你n,x,均不超过10^5,让你构造一个无重复元素的n个元素的非负整数集合(每个元素不超过10^6),使得它们的Xor和恰好为x. 如果x不为0: 随便在x里面找一个非零位,然后固定该位为0, ...

  6. CodeForces - 862C Mahmoud and Ehab and the xor(构造)

    题意:要求构造一个n个数的序列,要求n个数互不相同,且异或结果为x. 分析: 1.因为0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ (0 ^ 1 ^ 2 ^ 3 ...

  7. CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)

    <题目链接> 题目大意: 给定一个数n,代表有一个0~n-1的完全图,该图中所有边的边权为两端点的异或值,求这个图的MST的值. 解题分析: 数据较大,$10^{12}$个点的完全图,然后 ...

  8. CodeForces - 862C Mahmoud and Ehab and the xor(构造)【异或】

    <题目链接> 题目大意: 给出n.m,现在需要你输出任意n个不相同的数(n,m<1e5),使他们的异或结果为m,如果不存在n个不相同的数异或结果为m,则输出"NO" ...

  9. 【Codeforces Round #435 (Div. 2) C】Mahmoud and Ehab and the xor

    [链接]h在这里写链接 [题意] 让你组成一个n个数的集合,使得这n个数的异或和为x; x<=1e5 每个数最大1e6; [题解] 1e5<=2^17<=2^18<=1e6的 ...

  10. codeforces 862 C. Mahmoud and Ehab and the xor(构造)

    题目链接:http://codeforces.com/contest/862/problem/C 题解:一道简单的构造题,一般构造题差不多都考自己脑补,脑洞一开就过了 由于数据x只有1e5,但是要求是 ...

随机推荐

  1. CentOS8安装Docker报错问题解决

    问题描述 CentOS版本:8.5.2111. # cat /etc/redhat-release CentOS Linux release 8.5.2111 安装准备: # 安装所需软件包 sudo ...

  2. 2020-11-18 原生js实现自动打字效果

    原理 使用定时器,对要输出的文字进行遍历,每遍历一次,都增加一个字以及在段尾加上"|"暗示别人正在打字. js代码 const fangWrite = (theString, qu ...

  3. OsgEarth开发笔记(四):Qt5.15.2在QtCreator集成Osg3.6.3+OsgEarth3.1+OsgQt的vs2019x64版本开发环境搭建

    前言   本篇非常麻烦,博主用QtCreator作为IDE,因为Osg3.6.3放弃对osgQt的支持,集成起来比较繁琐.   前提   基于前面三篇的基础上,才可以进行本篇.   Demo演示:Qt ...

  4. 【Azure Developer】use @azure/arm-monitor sdk 遇见 ManagedIdentityCredential authentication failed.(status code 500)

    问题描述 在使用 @azure/arm-monitor sdk 创建 MonitorClient对象时候,遇见错误 ManagedIdentityCredential authentication f ...

  5. mysql-编写脚本-批量插入数据

    一.代码 -- 报工设置,添加数据 set @org_id = '租户id'; set @created_user = 'yike'; set @updated_user = 'yike'; set ...

  6. 一文搞定POI,再也不怕excel导入导出了

    写在前面 在Java日常开发过程中,实现Excel文件的导入导出功能是一项常见的需求. 通过使用相关的Java库,如Apache POI.EasyPoi或EasyExcel,可以轻松地实现Excel文 ...

  7. java.lang.Long cannot be cast to java.util.Map-Oracle查询异常处理

    Map<String, Object> map一.问题由来 测试环境中进行测试时,某一个接口频繁报一个错,java.lang.Long cannot be cast to java.uti ...

  8. Linux系统设置shell开机自启

        自己写一个shell脚本 chmod -x file.sh sudo cp file.sh /etc/profile.d/ 将写好的脚本(.sh文件)放到目录 /etc/profile.d/ ...

  9. Python Numpy 中的打印设置函数set_printoptions

    一 概述 np.set_printoptions()用于控制Python中小数的显示精度. 二 解析 np.set_printoptions(precision=None, threshold=Non ...

  10. python元组(tuple)循环遍历实例分析

    一 概念: 元组是有序且不可更改的集合.在 Python 中,元组是用圆括号编写的. 二 使用方法: 1  基本创建: thistuple = ("apple", "ba ...