码迷,mamicode.com
首页 > 其他好文 > 详细

gym101532 Subarrays Beauty

时间:2017-10-08 13:04:10      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:close   type   define   isp   img   include   show   一个   ace   

题意:输入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

标签:close   type   define   isp   img   include   show   一个   ace   

原文地址:http://www.cnblogs.com/Noevon/p/7636957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!