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的更多相关文章

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

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

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

  3. Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)

    Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...

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

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

  5. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

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

  7. 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的子序列 ...

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

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

随机推荐

  1. Hyperparameter tuning

    超参数调整 详细可以参考官方文档 定义 在拟合模型之前需要定义好的参数 适用 Linear regression: Choosing parameters Ridge/lasso regression ...

  2. 本地文件包含(LFI)漏洞

    PHP file://封装 PHP php://filter PHP ZIP封装LFI 通过/proc/self/environ执行LFI 空字节技术 截断LFI绕过 通过邮件给目标机器发送一个反弹s ...

  3. Jungle Roads HDU - 1301 prim

    #include<stdio.h> #include<string.h> #include<iostream> using namespace std; ; int ...

  4. 牛客寒假6-I 导航系统

    链接:https://ac.nowcoder.com/acm/contest/3007/I来源:牛客网 题目描述 小 Q 所在的国家有 N 个城市,城市间由 N-1 条双向道路连接,任意一对城市都是互 ...

  5. python3运行时候报错集锦:读取文件报错

    1.关于读取文件报错: 命令执行到cf.read(cfpath),出现如下报错:UnicodeDecodeError: 'gbk' codec can't decode byte 0xa0 in po ...

  6. Codeforces667D(spfa+dp)

    题意: 给定一个带权有向图,若P(A,B)表示节点A到B的最短路长度,选择四个节点ABCD,使得P(A,B)+P(B,C)+P(C,D)最大. 节点数n在1,000以内,边数m在2,000以内. 思路 ...

  7. Dubbo之服务消费

    Dubbo的服务消费主要包括两个部分.第一大步是ReferenceConfig类的init方法调用Protocol的refer方法生成Invoker实例,这是服务消息的关键.第二大步是把Invoker ...

  8. react-native简单使用

    基本组件的使用介绍 View: Text: TextInput: Image: Button: ActivityIndicator: ScrollView:这是一个列表滚动的组件 ListView:也 ...

  9. 0004 工程配置settings.py

    两个目录的区别: 工程目录是指包含manage.py文件的目录 配置目录是批包含settings.py文件的目录 在配置目录中找到并打工settings.py文件,做以下配置: 01 DEBUG DE ...

  10. Doing Homework HDU - 1074 状态压缩

    #include<iostream> #include<cstring> #include<cstdio> #include<string> #incl ...