<题目链接>

题目大意:

给定一个数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. Magento2.X 前端&综合 简要

    主题是Magento的应用程序,它提供了整个应用的前端部分: 主题旨在覆盖或自定义视图层资源,通过模块和库最初提供.主题由不同的供应商(前端开发人员)实施,并拟分配为类似于其他组件的Magento系统 ...

  2. centos7虚拟机下python3安装matplotlib遇到的一些问题

    1.安装位置 centos7虚拟机+python3.6 2.问题 2.1如果是使用的python2版本可以使用如下方式, #yum search matplotlib 返回如下: 已加载插件:fast ...

  3. OpenLayers学习笔记(九)— 限制地图显示范围

    openlayers 3 地图上限制地图显示及拖动范围,坐标系是4326转3857,中心经纬度精确到小数点后六位,减少误差 GitHub:八至 作者:狐狸家的鱼 本文链接:ol3-限制地图显示及拖动范 ...

  4. ubuntu:基本操作;

    1.系统时间与网络时间同步: sudo dpkg-reconfigure tzdata 注: 该博文为扩展型:

  5. Entity Framework入门教程(16)---Enum

    EF DbFirst模式中的枚举类型使用 这一节介绍EF DbFirst模式中的Enum(枚举类型),CodeFirst模式中的Enum会在以后的EF CoreFirst系列中介绍.EF5中添加了对E ...

  6. wxpython多线程间通信

    #!bin/bash/python # -*- coding=utf-8 -*- import time import wx from threading import Thread from wx. ...

  7. python GUI 之 tkinter

    写一个 登陆窗口来学习 tkinter ,还剩下一些问题 代码暂时如下 import tkinter as tk import webbrowser import pickle from tkinte ...

  8. 第十二节:MVC中的一些特殊优化

    一. 删除WebForm视图引擎 在MVC框架中检索视图的顺序为:当前控制器下对应的文件夹的aspx文件→share文件夹aspx文件→当前控制器下对应文件夹的cshtml文件→share文件夹的cs ...

  9. esnext:Function.prototype.toString 终于有规范了

    从 ES1 到 ES5 的这 14 年时间里,Function.prototype.toString 的规范一字未变: An implementation-dependent representati ...

  10. APPLE-SA-2019-3-25-4 Safari 12.1

    APPLE-SA-2019-3-25-4 Safari 12.1 Safari 12.1 is now available and addresses the following: Safari Re ...