CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)
<题目链接>
题目大意:
给定一个数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+找规律)的更多相关文章
- Codeforces 959E. Mahmoud and Ehab and the xor-MST 思路:找规律题,时间复杂度O(log(n))
题目: 解题思路 这题就是0,1,2...n-1总共n个数字形成的最小生成树. 我们可以发现,一个数字k与比它小的数字形成的异或值,一定可以取到k与所有正整数形成的异或值的最小值. 要计算n个数字的情 ...
- Codeforces 862C - Mahmoud and Ehab and the xor
862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...
- # 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(构造)【异或】
<题目链接> 题目大意: 给出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 ...
随机推荐
- Flask 模型操作
ORM 简称 ORM, O/RM, O/R Mapping 持久化模型 特征 数据表和编程类的映射 数据类型映射 字段属性映射类的属性和数据类型 关系映射 表于表之间的关系对应到 编程类的关系上 优点 ...
- mysql ssh 跳板机(堡垒机???)连接服务器
跳板机(Jump Server),也称堡垒机,是一类可作为跳板批量操作远程设备的网络设备,是系统管理员或运维人员常用的操作平台之一. 正常的登录流程 使用ssh命令登录跳板机: 登录跳板机成功后,在跳 ...
- jpg、png格式的图片转换成webp后颜色失真的问题
今天简单的试用了一下 cweb.exe 将 jpg, png 格式的图片转换成 webp 格式. 我今天下载的是当前最新版:1.0.0 cwebp 3.jpg -q 85 -o 3.webp 发现图 ...
- Mac新手必看教程—让你离熟练操作mac只差十分钟
本文收录于:风云社区(提供各类mac软件资源下载) 本文源自:什么值得买 无论轻薄办公本.还是赶超台式性能的游戏本,关注#笔记本攻略#栏目,解决笔记本电脑从选购到使用的各种问题. 引子 大部分用户接触 ...
- JavaScript null和undefined的区别
前言 1995年javascript诞生时,最初像Java一样,只设置了null作为表示"无"的值.根据C语言的传统,null被设计成可以自动转为0 但是,javascript的设 ...
- 使用 functional interface 和 lambda 表达式来优化代码
========================================原始代码========================================RoleService 类有删除 ...
- springMVC中 @RequestParam和@RequestBody的区别
首先,不可以同时传进@RequestParam和@RequestBody,好像可以传进两个@RequestParam 如果不加@requestparam修饰,相当于 加上@requestparam且各 ...
- 【搞事情】VS2015下的openGL初始化
环境:glfw+glew+visual studio 2015 原材料下载链接: glfw 下载 glew 下载 glm库 下载 cmake 下载 (我下载的时候有些官网戳不开(大概校园网问题)... ...
- sessionStorage数组、对象的存储和读取
一个对象的demo如下: var obj = { name:"name", age:18, love:"美女" } sessionStorage.setItem ...
- 【先验知识归纳】Flask快速入门
本文参考:快速入门 - Flask 0.10.1 文档 路由 Flask使用route修饰器来关联URL与程序函数: @app.route('/') def hello_world(): return ...