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

解题思路
这题就是0,1,2...n-1总共n个数字形成的最小生成树。
我们可以发现,一个数字k与比它小的数字形成的异或值,一定可以取到k与所有正整数形成的异或值的最小值。
要计算n个数字的情况我们可以通过n-1个数字的情况得来,意为前n-1个数字的最小生成树已经生成好了,我们需要给第n个数字连一条边,使新的树为n个数字的最小生成树。
通过找规律我们可以发现:
- 每隔2个数字多一个权值为1的边。
- 每隔4个数字多一个权值为2的边。
- 每隔8个数字多一个权值为4的边。
- ……
- 每隔2^n个数字多一个权值为2^(n-1)的边。

我们把这些边加起来可以推出这样一个公式:

注意除以2^(i+1)和乘2^i不能直接抵消,因为这里的数字全是int型,没有小数。
时间复杂度:
O(log(n))
代码:
#include<bits\stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n;
while(cin >> n){
n--;
int m = log(n)/log();
ll ans = ;
for(int i = ;i <= m; i++){
ans += ((ll)(n+pow(,i))/(ll)pow(,i+))*(ll)pow(,i);
}
cout << ans << endl;
}
return ;
}
Codeforces 959E. Mahmoud and Ehab and the xor-MST 思路:找规律题,时间复杂度O(log(n))的更多相关文章
- CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)
<题目链接> 题目大意: 给定一个数n,代表有一个0~n-1的完全图,该图中所有边的边权为两端点的异或值,求这个图的MST的值. 解题分析: 数据较大,$10^{12}$个点的完全图,然后 ...
- # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor
E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...
- Codeforces 862C - Mahmoud and Ehab and the xor
862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...
- CodeForces - 862C Mahmoud and Ehab and the xor(构造)【异或】
<题目链接> 题目大意: 给出n.m,现在需要你输出任意n个不相同的数(n,m<1e5),使他们的异或结果为m,如果不存在n个不相同的数异或结果为m,则输出"NO" ...
- Codeforces.959E.Mahmoud and Ehab and the xor-MST(思路)
题目链接 \(Description\) 有一张\(n\)个点的完全图,从\(0\)到\(n-1\)标号,每两点\(i,j\)间的边权为\(i\oplus j\).求其最小生成树边权之和. \(Sol ...
- CodeForces - 862C Mahmoud and Ehab and the xor(构造)
题意:要求构造一个n个数的序列,要求n个数互不相同,且异或结果为x. 分析: 1.因为0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ (0 ^ 1 ^ 2 ^ 3 ...
- 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 ...
- Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)
Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...
- Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)
Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...
随机推荐
- TCP和UDP的具体区别
TCP和UDP的最完整的区别 TCP UDP TCP与UDP基本区别: 1.基于连接与无连接 2.TCP要求系统资源较多,UDP较少: 3.UDP程序结构较简单 4.流模式(TCP)与数据报模式(UD ...
- COM基础
为什么说COM的可重用性是建立在二进制级别? COM本身是语言无关,它的标准建立在二进制级别.对于使用COM组件的客户程序,它只需要要使用的COM对象信息就可以通过COM库的帮助创建和使用COM对象, ...
- Android跳转到系统Wifi界面的方式
第一种 Intent intent = new Intent(); intent.setAction("android.net.wifi.PICK_WIFI_NETWORK"); ...
- URL回车后发生了什么
1.解析URL ________________________________________________________________________ 关于URL: URL(Universa ...
- BZOJ 2820: YY的GCD 莫比乌斯反演_数学推导_线性筛
Code: #include <cstdio> #include <algorithm> #include <cstring> #include <vecto ...
- 连连看 HDU - 1175_搜索_剪枝
hdu有毒,考试上 AC 的就是一直 WA- 其实这道题是可以进行初始化来进行优化的,这样的话询问次数是可以达到 10510^5105 的.不过普通的 dfsdfsdfs + 剪枝也是可过的. Cod ...
- ntp.log日志梳理
[日志]offset 正负 机器A上执行: remote refid st t when poll reach delay offset jitter ======================== ...
- php中mysqli 处理查询结果集总结
在PHP开发中,我们经常会与数据库打交道.我们都知道,一般的数据处理操作流程为 接收表单数据 数据入库 //连接数据库 $link = mysqli_connect("my_host&quo ...
- MVC、RPC、SOA、微服务架构之间的区别
MVC.RPC.SOA.微服务架构之间的区别 一.MVC架构 其实MVC架构就是一个单体架构. 代表技术:Struts2.springMVC.Spring.Mybatis 等等. 二.RPC架构 RP ...
- JS侧边栏实现
<!DOCTYPE html> <html lang="en"> <style> </style> <head> < ...