CF 959 E. Mahmoud and Ehab and the xor-MST

E. Mahmoud and Ehab and the xor-MST

https://codeforces.com/contest/959/problem/E

分析:

  每个点x应该和x ^ lowbit(x)连边,那么现在就是求$\sum_{i=1}^{n}lowbit(i)$,然后打表找规律。

代码:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<cmath>
 6 #include<cctype>
 7 #include<set>
 8 #include<queue>
 9 #include<vector>
10 #include<map>
11 using namespace std;
12 typedef long long LL;
13 
14 inline int read() {
15     int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
16     for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';return x*f;
17 }
18 
19 int main() {
20     LL n, res = 0;
21     cin >> n; n--;
22     for (LL x = 1; x <= n; x <<= 1) 
23         res += ((n - x) / (x + x) + 1) * x;
24     cout << res;
25     return 0;
26 }

 

posted @ 2018-12-05 14:36  MJT12044  阅读(276)  评论(0编辑  收藏  举报