gym101532 Subarrays Beauty

Posted 2855669158

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gym101532 Subarrays Beauty相关的知识,希望对你有一定的参考价值。

题意:输入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\n", ret);
    }
    return 0;
}
View Code

 

以上是关于gym101532 Subarrays Beauty的主要内容,如果未能解决你的问题,请参考以下文章

930. Binary Subarrays With Sum

1031. Maximum Sum of Two Non-Overlapping Subarrays

689. Maximum Sum of 3 Non-Overlapping Subarrays

LeetCode 689. Maximum Sum of 3 Non-Overlapping Subarrays

[LeetCode] 1031. Maximum Sum of Two Non-Overlapping Subarrays

Leetcode 1630. Arithmetic Subarrays