<题目链接>

题目大意:

给定一个数n,代表有一个0~n-1的完全图,该图中所有边的边权为两端点的异或值,求这个图的MST的值。

解题分析:

数据较大,$10^{12}$个点的完全图,然后异或又暂时推不出什么性质,所以先起手Kruskal打一张小数据完全图的MST的表,发现规律其实还是蛮好找的。

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5+;

int fa[N],cnt,ncase;
struct Edge{ int u,v,w;
bool operator < (const Edge &tmp)const{
return w<tmp.w;
}
}e[N];
map<int,int>mpa;
inline void add(int u,int v,int w){
e[++cnt]=(Edge){u,v,w};
}
inline int find(int &x){
while(x!=fa[x])
x=fa[x]=fa[fa[x]];
}
inline void Kruskal(){
sort(e+,e++cnt);
for(int i=;i<=cnt;i++){
int u=e[i].u,v=e[i].v;
find(u);find(v);
if(u!=v){
fa[v]=u;
printf("%dth edge , w=%d\n",++ncase,e[i].w);
mpa[e[i].w]++;
}
}
for(auto i:mpa){
printf("%d have %d (num)\n",i.first,i.second);
}
}
int main(){
int n;cin>>n;
ncase=;
for(int i=;i<n;i++)fa[i]=i;
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
add(i,j,i^j);
}
}
Kruskal();
}

打表

然后根据规律就可以快速求解了。

#include <bits/stdc++.h>
using namespace std; typedef long long ll; int main(){
ll n,ans,w=;
scanf("%lld",&n);
while(n>){
ans+=w*(n>>); //(n>>1)代表边数,w代表权值
w<<=;
n-=n>>;
}
cout<<ans<<endl;
}

CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)的更多相关文章

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

    题目: 解题思路 这题就是0,1,2...n-1总共n个数字形成的最小生成树. 我们可以发现,一个数字k与比它小的数字形成的异或值,一定可以取到k与所有正整数形成的异或值的最小值. 要计算n个数字的情 ...

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

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

  3. # E. Mahmoud and Ehab and the xor-MST dp/数学+找规律+xor

    E. Mahmoud and Ehab and the xor-MST dp/数学/找规律 题意 给出一个完全图的阶数n(1e18),点由0---n-1编号,边的权则为编号间的异或,问最小生成树是多少 ...

  4. CodeForces - 862C Mahmoud and Ehab and the xor(构造)【异或】

    <题目链接> 题目大意: 给出n.m,现在需要你输出任意n个不相同的数(n,m<1e5),使他们的异或结果为m,如果不存在n个不相同的数异或结果为m,则输出"NO" ...

  5. Codeforces.959E.Mahmoud and Ehab and the xor-MST(思路)

    题目链接 \(Description\) 有一张\(n\)个点的完全图,从\(0\)到\(n-1\)标号,每两点\(i,j\)间的边权为\(i\oplus j\).求其最小生成树边权之和. \(Sol ...

  6. CodeForces - 862C Mahmoud and Ehab and the xor(构造)

    题意:要求构造一个n个数的序列,要求n个数互不相同,且异或结果为x. 分析: 1.因为0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ (0 ^ 1 ^ 2 ^ 3 ...

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

  8. Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)

    Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...

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

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

随机推荐

  1. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  2. jenkins在windows及linux环境下安装

    下载 下载地址: https://jenkins.io/download/ 下载windows和linux通用的war包 jenkins在windows下安装 前提:已经安装jdk.tomcat 将w ...

  3. A.01.12—模块的输出—通讯(CAN&LIN)

    AN和LIN相关的内容很多,今天仅对几年前困扰过我的一个疑问进行说明. 以前最常见的通迅方式为CAN和LIN,但现在也有很多其他的通讯方式了,而这两种通讯方式仍使用广泛. 前几年常听人说CAN的成本和 ...

  4. Codeforces 1037E Trips

    原题 题目大意: 有\(n\)个人,起初他们都不是朋友.总共有\(m\)天,每天会有两个人成为朋友.他们计划在晚上出去旅游,对于一个人,有如下两种情况: 1.要么他不出去旅游 2.要么有至少\(k\) ...

  5. Python爬虫之一

    1. 爬虫的选取:scrapy和requests+beautifuisoup scrapy是框架,而requests和beautifulsoup是库.scrapy框架是可以加如requests和bea ...

  6. 剑指Offer_编程题_19

    题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2, ...

  7. Numpy系列(一)- array

    初始Numpy 一.什么是Numpy? 简单来说,Numpy 是 Python 的一个科学计算包,包含了多维数组以及多维数组的操作. Numpy 的核心是 ndarray 对象,这个对象封装了同质数据 ...

  8. css预编译语言 sass scss(变量$var, css嵌套规则,@import规则,@extend,@mixin)

    什么是sass Sass 是对 CSS 的扩展,让 CSS 语言更强大.优雅. 它允许你使用变量.嵌套规则. mixins.导入等众多功能, 并且完全兼容 CSS 语法. Sass 有助于保持大型样式 ...

  9. 异常捕获try----catch

    如果try语句里有return,返回的是try语句块中变量值. 详细执行过程如下: 如果有返回值,就把返回值保存到局部变量中: 执行jsr指令跳到finally语句里执行: 执行完finally语句后 ...

  10. BZOJ-2308 小z的袜子(莫队)

    题目链接 题意 $n$点$m$次询问区间内随机取两个数是相同数的概率 思路 莫队入门题,对询问按块排序后更新答案,复杂度$O(n\sqrt{n})$ 代码 //#pragma comment(link ...