题目链接

\(Description\)

  有一张\(n\)个点的完全图,从\(0\)到\(n-1\)标号,每两点\(i,j\)间的边权为\(i\oplus j\)。求其最小生成树边权之和。

\(Solution\)

  为方便,以下点从\(0\)到\(n\)编号。
  每个点\(x\)应和\(x\oplus lowbit(x)\)相连,边权为\(lowbit(x)\)(\(lowbit(x)\)会和\(0\)相连,所以一定能构成树),所以答案为\(\sum_{i=1}^nlb(i)\)。
  继续优化。注意到\(lb(i)\)一定是某个2次幂,所以令\(f(i)\)表示\(1\leq x\leq n\)且满足\(lb(x)=i\)的\(x\)的个数,则答案为\(\sum_{i=1}^nf(i)\times i\ (f(i)>0)=\sum_{i=0}^{\lfloor\log n\rfloor}f(2^i)\times 2^i\)
  \(f(i)\)显然可以用数位DP算,但是太麻烦了。。
  一些满足\(lb(i)=x\)的数,它们间隔至少是\(2x\)。比如\(x=(100)_2\),则\(i=100,1100,10100...\)(相差\(1000\))。所以\(f(x)=\lfloor\frac{n-x}{2x}\rfloor+1\ (1\leq x\leq n,x=2^y)\)。


  还有DP求\(\sum_{i=1}^nlb(i)\)的做法,好长啊...先不看了。


#include <cstdio>int main()
{long long n,res=0;scanf("%I64d",&n); --n;for(long long x=1; x<=n; x<<=1)res+=x*((n-x)/(x<<1)+1);printf("%I64d\n",res);return 0;
}

转载于:https://www.cnblogs.com/SovietPower/p/9345811.html

Codeforces.959E.Mahmoud and Ehab and the xor-MST(思路)相关推荐

  1. Codeforces 862B - Mahmoud and Ehab and the bipartiteness

    862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...

  2. Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)

    <题目链接> 题目大意: 有一个长度为n(n<1000)的01串,该串中至少有一个0和一个1,现在由你构造出一些01串,进行询问,然后系统会给出你构造的串与原串的   Hamming ...

  3. Codeforces round #628 C.Ehab and Path-etic MEXs

    Codeforces round #628 C.Ehab and Path-etic MEXs You are given a tree consisting of n nodes. You want ...

  4. Codeforces 766E Mahmoud and a xor trip(树形DP)

    题目链接 Mahmoud and a xor trip 树形DP.先考虑每个点到他本身的距离和,再算所有点两两距离和. 做的时候考虑二进制拆位即可. #include <bits/stdc++. ...

  5. D. Mahmoud and Ehab and the binary string Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/D 交互题 fflush(stdout) 调试: 先行给出结果,函数代替输入 1 #include <cstd ...

  6. E. Mahmoud and Ehab and the function Codeforces Round #435 (Div. 2)

    http://codeforces.com/contest/862/problem/E 二分答案 一个数与数组中的哪个数最接近: 先对数组中的数排序,然后lower_bound 1 #include ...

  7. Codeforces 959 E. Mahmoud and Ehab and the xor-MST 思路:找规律题,时间复杂度O(log(n))

    题目: 解题思路 这题就是0,1,2-n-1总共n个数字形成的最小生成树. 我们可以发现,一个数字k与比它小的数字形成的异或值,一定可以取到k与所有正整数形成的异或值的最小值. 要计算n个数字的情况我 ...

  8. 【CodeForces - 289E 】Polo the Penguin and XOR operation (数学,异或,贪心)

    题干: Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 ...

  9. [Codeforces Round #628]1325C - Ehab and Path-etic MEXs[思维][图]

    1325C - Ehab and Path-etic MEXs[思维][图] time limit per test memory limit per test input output 1 seco ...

最新文章

  1. pyqt5程序发生错误不中断_关于Windows页面错误的一些基础概念
  2. 项目上线简化流程介绍
  3. 祝「杭州程序媛」母亲节快乐!
  4. 互联网协议 — TLS — 使用 OpenSSL 自建 CA 中心
  5. mybatis plus 日志打印_mybatis升级为mybatis-plus踩到的坑
  6. 邬贺铨:区块链技术将确保物联网隐私和安全
  7. 代码中特殊的注释技术——TODO、FIXME和XXX的用处
  8. 小程序分享朋友圈_改造小程序,增加分享朋友圈代码
  9. boost::log模块测量日志记录发射的性能
  10. vue 分享微信传参_vue实现微信分享链接添加动态参数的方法
  11. Python标准库判断图片文件和声音文件的格式
  12. 自动化用例设计原则+web自动化框架
  13. Tslib的触摸屏5点校准算法原理和实现
  14. 三步棋,跟着bit鹏哥学得
  15. 用accelstepper库控制28BYJ-48步进电机(快慢运动切换)
  16. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
  17. python用电度数设计_用Python实现一个爬取XX大学电费通知的小脚本
  18. 前端2020面试题195道
  19. bbqsql安装使用踩坑总结
  20. python 图像批量png转jpg格式

热门文章

  1. “跟着菜鸟一起学R语言” 现已更名为“数据志”
  2. 惠普打印机怎么无线连接电脑_惠普打印机打印中突然停止怎么办?
  3. go 函数参数nil_深入理解 Go-Defer的机制
  4. 深度linux创建微信图标,Deepin Linux 下基于deepin-wine的微信图标不见的问题解决
  5. sql server排序慢_用Nginx实现接口慢查询并可示化展示TOP 20
  6. php 带下划线的函数,[宜配屋]听图阁
  7. android r类 作用,Android 主项目和 Module 中 R 类的区别
  8. php 替换回车tab,PHP替换回车换行符的三种方法
  9. python2和python3分别是python的两个版本_Windows下Python2与Python3两个版本共存的方法详解...
  10. 2.QML组件、图像几何变换和元素定位器