# 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 ...
随机推荐
- sql-labs 18-20(sqlmap注入)
这三题主要是关于HTTP头部的注入 常见的HTTP注入点产生位置为 [Referer].[X-Forwarded-For].[Cookie].[X-Real-IP].[Accept-Language] ...
- 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(6.5-加-8.2-加-8.3)End-to-end_Data_Protection
6.5 End-to-end Protection Information 端到端保护信息 包含数据转存的命令可以使用端到端数据保护.在这些命令内部[位于Command Dword 12 的 [29: ...
- 树莓派中安装ubuntu及相关设置
一.下载并烧录系统 首先准备好我们要烧录的ubuntu_meta系统,可以在树莓派官网中下载https://www.raspberrypi.org/downloads/ 这里我们选择 Raspberr ...
- 微信小程序 获取用户昵称、头像
前段时间微信小程序对获取用户昵称和头像方法进行了更新,网上很多的文章都已经不适用了,这里简单总结一下,首先,传统接口wx.getUserInfo的效果会弹出一个给用户的弹窗,需要用户授权,经过测试传统 ...
- createElement(九)
Vue.js 利用 createElement 方法创建 VNode,它定义在 src/core/vdom/create-elemenet.js 中: // wrapper function for ...
- Maven无法下载com.oracle:ojdbc14:jar解决方法
问题说明:导入Maven项目时,想要添加Oracle驱动包时,Maven的pom.xml总是报Missing artifact com.oracle:ojdbc14:jar:10.2.0.1.0错. ...
- String.format()的用法记录
stirng.format("redirect:http://%s:%s%s",ip,port,path) 相当于http://localhost:8080/user/list S ...
- pytorch之max()函数
pytorch之max()函数 待办 返回对应给定中最大值的索引,方便进行和target结果的索引进行比较 索引方式见下 https://blog.csdn.net/liuweiyuxiang/art ...
- ubuntu---【nvcc --version】显示错误,提示 sudo apt-get install nvidia-cuda-toolkit
重装了一下cuda,然后发现nvcc命令不存在了,终端提示使用 : sudo apt-get install nvidia-cuda-toolkit 来使用nvcc. 注意不要使用这种方式安装.系统认 ...
- 解决报错Failed to start LSB: Bring up/down networking:MAC地址导致
1.场景描述,我在电脑里装好的虚拟机,今天突然就网络无法打开,并出现如下报错: [root@ansible-control ~]# systemctl start networkJob for net ...