[CF959E] Mahmoud and Ehab and the xor-MST - greedy, minimum spanning tree

\ (n-\) completely icon number points \ ((n---0. 1) \) , \ (I \) and \ (J \) connected edge weight value \ (i \ \ textrm {XOR } \ j \) , evaluation of MST

Solution

Set \ (f [n] \) represented by points is \ (n + 1 \) answer, the chances greedy considered, obviously \ (f [0] = 0 , f [n] = f [n-1] + lowbit (n) \)

The observation readily available \ (F [n-] =. 2F [n--. 1] + 2 ^ n--2 ^ {n--. 1} \) , and because \ (F [] \) is a \ (lowbit \) prefix and meet additive, so directly \ (n \) binary decomposition and statistics to answer

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 1000005;
int f[N],n,ans;

signed main() {
    cin>>n;
    --n;
    f[0]=1; f[1]=3; f[2]=8;
    for(int i=3;i<=40;i++)
        f[i]=f[i-1]*2-(1ll<<(i-1))+(1ll<<i);
    for(int i=0;i<=40;i++) if(n&(1ll<<i)) ans+=f[i];
    cout<<ans;
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12586581.html