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. Codeforces Round #624 (Div. 3)

    A.题意:通过加奇数减偶数的操作从a到b最少需要几步 签到题 #include <algorithm> #include <iostream> #include <cst ...

  2. Deep Clustering Algorithms

    Deep Clustering Algorithms 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 本文研究路线:深度自编码器(Deep Autoen ...

  3. Redis03——Redis之单线程+多路IO复用技术

    Redis 是单线程+多路IO复用技术 多路复用:使用一个线程来检查多个文件描述符的就绪状态 如果有一个文件描述符就绪,则返回 否则阻塞直到超时 得到就绪状态后进行真正的操作可以在同一个线程里执行,也 ...

  4. linux-crond_计划任务

    定时计划任务 主要文件介绍: [root@nginx ~]# ll /etc/cron* -d drwxr-xr-x. 2 root root 21 7月 11 20:28 /etc/cron.d d ...

  5. 图灵,咕泡,鲁班学院--Java高级架构师-互联网企业级实战VIP课程(价值6380)

    课程介绍:        讲课内容涉及Java互联网技术工程框架.应用框架.        性能调优 (Tomcat Nginx JVM)         分布式框架(并发编程 Zookeeper N ...

  6. ajax从jsp向servlet传值

    function Delete(s){ xmlHttp=new XMLHttpRequest(); var url = "/emp/FindSpecial?selcol=" +s; ...

  7. Nginx防止DDOS流量攻击

    DDOS流量攻击:频繁的发送请求,造成宽带占用,其他客户端无法访问                Nginx解决DDOS流量攻击,利用limit_req_zone限制请求次数    limit_con ...

  8. join方法,wait()和sleep()

    join方法解释:方法x.join()的作用是使所属线程x 正常执行run()中的方法,而使得调用x.join()的线程处于无限期阻塞状态,等待x线程销毁后再继续执行线程z后面的代码. 1.方法joi ...

  9. 理解 nodeJS 中的 buffer,stream

    在Node.js开发中,当遇到 buffer,stream,和二进制数据处理时,你是否像我一样,总是感到困惑?这种感觉是否会让你认为不了解它们,以为它们不适合你,认为而这些是Node.js作者们的事情 ...

  10. JUC-多线程锁

    多线程锁的练习题 1.标准访问,先打印短信还是邮件 class Phone { public synchronized void sendSMS() throws Exception { Thread ...