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 ...
随机推荐
- 用Pytorch训练线性回归模型
假定我们要拟合的线性方程是:\(y=2x+1\) \(x\):[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] \(y\):[1, 3, 5, 7, ...
- mysql 重启,修改编码utf8mb4,并修改数据库链接,生效
1.启动:/etc/init.d/mysql start 2.停止:/etc/init.d/mysql stop 3.重启:/etc/init.d/mysql restart SHOW VARIABL ...
- (BFS) leetcode 279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- Gaussian Process for Regression
python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...
- Flask Web中文教程
Flask Web中文教程:http://docs.jinkan.org/docs/flask/
- SpringBoot系列: Pebble模板引擎
===============================Java 模板引擎选择===============================SpringBoot Starter项目向导中可选的J ...
- 关于MySql经典高频查询语句的整理
一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!>,!<,=>,= ...
- Codeforces Round #449 (Div. 2) D. Ithea Plays With Chtholly
题目链接 交互题. 题意:给你三个数n,m,k.让你完成至多m次互动,每次给你一个q,让你从n个位置选一个位置放这个数,覆盖已经放过的数.让你再m次使得n个位置的数不递减,达到直接退出. 解法:暴力, ...
- 4.Centos7安装JDK8以及环境配置
1.下载 linux 版本 jdk (jdk-8u11-linux-x64.tar.gz) 一定要是 .tar.gz 版本,可以去我的百度网盘下载或者在百度上面找 2.新建文件夹命令:mkdir /u ...
- git切换到新的远程地址
查看仓库链接 git remote -v 修改url链接 git remote set-url origin URL