# E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor
E. Mahmoud and Ehab and the xor-MST dp/数学/找规律
题意
给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少
思路
由于一个数k和比他小的数异或,一定可以取到k与所有正整数形成的异或值的最小值。这里简单得形式化证明一下 假设一个数为1000110 那么他的最佳异或和为010(即留下最靠近右边的1其他全部置0) 我们定义\(lsb(x)=x\And(-x)\)由字符形的变量编码我们可以知道,这就可以取得x最右边的那个值 所以只要从小到大加点,每次加一个点的时候,选取比它小的编号的点和它异或的最小边相连,一定可以构成最小的生成树每次加的边都是\(lsb(x)\)
所以计算为 \(\sum_{i=0}^{n-1}lsb(i)\) 现在考虑的就是如何优化了,因为n<=1e18,盲猜\(O(logn)\)复杂度
数学法
设\(f(x)\)为整数y满足\(1<=y<=n\)并且\(lsb(y)=x\)的数有多少,所以\(\sum_{i=1}^{n}lsb(i)=\sum_{i=1}^{n}i×f(i)\)由当且仅当i为2的幂的时候f(x)>0所以公式转化成\(\sum_{i=1}^{log(n)}f(2^i)*2^i\) 而由二进制由4举例 100 1100 11100 我们可以看出 每次取4的边都是要100的后缀所以1000就是其循环节 也就是2*(lsb(i));所以我们也就可以利用这个性质求出\(f(x)=\lfloor(n-x)/(2*x)\rfloor+1\) \(1<=x<=n\)x为\(2^k\)
dp法
由取边的大小\({1}->{1,2,1}->{1 ,2 ,1 ,4 ,1 ,2 ,1 ,8 ,1 ,2 ,1, 4, 1, 2, 1}\)
设f(x)=\(\sum_{i=1}^{x}lsb(x)\) 并设\(dp[i]=f(2^i-1)\)由上面的规律我们们可以看出\(dp[i]=2*dp[i-1]+2^{i-1}\)}即每次增加\(2^i-1\)个,都是左右复制一下上一个\(2^{i-1}-1\)然后中间多一个\(2^{i-1}\) 所以如果取点的数量是\(2^k\)的,就直接可以通过其算出来,那不是\(2^k\)长度的怎么办呢?我们将其分成两个部分\(f(x)=f(msb(x))+f(msb(x)\bigoplus x )\)
msb表示只取该数二进制位最右边的值,也就是前面说的\(2^k\),可以直接通过dp数组求出来,而剩下那部分,我们可以递归求解重复分界部分,这样分解到最后 其实就是n的每一位如果是1 那么就加上对应的这部分的值,例如\(f(1101_2)=f(1_2)+f(100_2)+f(1000_2))\)
数学法
#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
typedef long long ll;
using namespace std;
const int maxn=2e6+200;
int main(){
ll n,ans=0;
scanf("%lld",&n);
n--;
for(ll i=1;i<=n;i<<=1)
ans+=((n-i)/(i*2)+1)*i;//每个值都是2的不同幂产生的 值为lsb(a&(-a));即取最小位的1的值
printf("%lld\n",ans);
return 0;
}
dp法
#include<bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
typedef long long ll;
using namespace std;
const int maxn=2e6+200;
ll dp[200];
int main(){
ll n,ans=0;
scanf("%lld",&n);
n--;
for(int i=1;i<40;i++){
dp[i]=2ll*dp[i-1]+(1ll<<(i-1));
}
for(int i=0;i<40;i++){
if(n&(1ll<<i))ans+=dp[i]+(1ll<<i);
}
cout<<ans<<endl;
return 0;
}
# E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor的更多相关文章
- CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)
<题目链接> 题目大意: 给定一个数n,代表有一个0~n-1的完全图,该图中所有边的边权为两端点的异或值,求这个图的MST的值. 解题分析: 数据较大,$10^{12}$个点的完全图,然后 ...
- CF959D Mahmoud and Ehab and another array construction task 数学
Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same l ...
- Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)
Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...
- Codeforces 862C - Mahmoud and Ehab and the xor
862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- 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 ...
- 959F - Mahmoud and Ehab and yet another xor task xor+dp(递推形)+离线
959F - Mahmoud and Ehab and yet another xor task xor+dp+离线 题意 给出 n个值和q个询问,询问l,x,表示前l个数字子序列的异或和为x的子序列 ...
- Codeforces 862A Mahmoud and Ehab and the MEX
传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...
- E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)
Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipa ...
随机推荐
- Math Magic ZOJ - 3662
核心是要想到只枚举最小公倍数的因子 因为转移过程中一单添加了不是最小公倍数的因子,那么结果必然不合法,虽然最终答案是对的,但是这样的答案根本用不上,反而时间复杂度大大增加 #include<cs ...
- 使用mysql8.+版本,使用mybatis的代码生成工具:mybatis-generator连接数据库时Unknown initial character set index '255' received from server. Initial client character set can be forced via the 'characterEncoding' property.
Error connecting to database: (using class org.gjt.mm.mysql.Driver)Unknown initial character set ind ...
- C# 引入Sqlite 未能加载文件或程序集“System.Data.SQLite
个人博客 地址:https://www.wenhaofan.com/article/20190501224046 问题 在Visual Studio 中 使用NuGet 通过 install-pack ...
- hadoop基本组件原理小总结
Hadoop基础知识小总结 这是本人(学生党)在学习hadoop半个学期后根据教科书后习题做的一个小总结,如有发现错误还请各位海涵并指出,我会及时改过来的,谢谢! 目录 Hadoop基础知识小总结. ...
- js秒数倒计时
代码 /** * 调用回调函数 * @param callback 回调函数体 * @param args 参数 */ execCallback: function (callback, args) ...
- data_analysis 第一课
1.anaconda的安装与使用 在官网下载anaconda的客户端,因为python有2和3之分,所以有两个版本可以供选择,由于该课程使用2作为开发工具,选择anaconda2下载安装. 安装好之后 ...
- Selenium3+python自动化013-自动化数据驱动及模型介绍
一.查看当前运行的浏览等相关信息 driver=webdriver.Chrome() print(driver.capabilities["version"]) #浏览器版本 pr ...
- 全栈高级web前端工程师的必经之路
这是最近整理的一个进阶高级web工程师的技术栈 同时也是激励自己每天来看一下离真正的王者还有多少距离! 过段时间再来反思一下自己进步了多少? ------------------------20190 ...
- HDU1241 Oil Deposits(dfs+连通块问题)
背景描述 ztw同志负责探测地下石油储藏.ztw现在在一块矩形区域探测石油.他通过专业设备,来分析每个小块中是否蕴藏石油.如果这些蕴藏石油的小方格相邻(横向相邻,纵向相邻,还有对角相邻),那么它们被认 ...
- mysql的优化总结
1,允许数据库适当冗余提高查询效率 2,对于索引的使用要适当