欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CodeForces 959 E Mahmoud and Ehab and the xor-MST(异或 思维)

程序员文章站 2023-12-27 08:37:51
...

Description

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 vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex 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.

Input

The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.

Output

The only line contains an integer x, the weight of the graph’s minimum spanning tree.

Example

input
4
output
4

题目大意

一个完全图,每两个点之间的权值是两个节点编号相异或的值,求最小生成树.

解题思路

CodeForces 959 E Mahmoud and Ehab and the xor-MST(异或 思维)

代码实现

#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
typedef long long ll;
int main()
{
    IO;
    ll n;
    cin>>n;
    ll t=2,ans=0;
    while(n/t)
    {
        ll x=(t/2-1);
        ans+=((n+x)/t)*(t/2);
        t*=2;
    }
    t/=2;
    if(n%t!=0)
        ans+=(t&(-t));
    cout<<ans<<endl;
    return 0;
}

上一篇:

下一篇: