codeforces 959E Mahmoud and Ehab and the xor-MST

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<v<n 0<=u<v<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
#include
#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;
}

你可能感兴趣的:(模拟,数学)