codeforces 959E Mahmoud and Ehab and the xor-MST

84 篇文章 0 订阅

http://www.elijahqi.win/archives/2945
题目描述

Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of

n

n vertices numbered from

0

0 to

n-1

n−1 . For all 0<=u&lt;v&lt;n 0<=u&lt;v&lt;n , vertex

u

u and vertex

v

v are connected with an undirected edge that has weight (where is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph?

You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graph

You can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_tree

The weight of the minimum spanning tree is the sum of the weights on the edges included in it.

输入输出格式

输入格式:

The only line contains an integer

n

n

(2<=n<=10^{12})

(2<=n<=1012) , the number of vertices in the graph.

输出格式:

The only line contains an integer

x

x , the weight of the graph’s minimum spanning tree.

输入输出样例

输入样例#1: 复制

4
输出样例#1: 复制

4
说明

In the first sample: The weight of the minimum spanning tree is 1+2+1=4.

很有趣的一题 模拟kruskal的过程 手玩即可发现每次我的每个二进制位会贡献当前总数/2这么多的贡献 然后也相当于合并了这么些连通块 然后用总数减去合并的这些连通块再重复以上步骤

#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll n,ans,base=1;
int main(){
    freopen("cf959e.in","r",stdin);
    scanf("%lld",&n);
    while(n>1){
        ans+=base*(n>>1);base<<=1;n-=n>>1;
    }printf("%lld\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值