题意:输入n个数,问这n个数n*(n+1)/2所有的按位与的和
题解:把n个数拆为2进制,按位与的有一个0那么答案就是0, 从最低位开始算起,连续n个1所在的n*(n+1)/2个区间答案不为0
#include <bits/stdc++.h> #define maxn 100100 #define INF 0x3f3f3f3f typedef long long ll; using namespace std; ll a[maxn]; int main(){ ll n, ret, T; scanf("%lld", &T); while(T--){ ret = 0; scanf("%lld", &n); for(ll i=0;i<n;i++){ scanf("%lld", &a[i]); } ll bit = 1; for(ll j=0;j<20;j++){ ll ans = 0, sum = 0; for(ll i=0;i<n;i++){ if(a[i]&(1<<j)) ans++; else{ sum += ans*(ans+1)/2; ans = 0; } } sum += ans*(ans+1)/2; ret += sum*bit; bit*=2; } printf("%lld ", ret); } return 0; }