ZUFE 问题 P: 感受风的韵律

数据量太大了,不能暴力莽过,但是抑或运算每个二进制的值相对独立,对了,别用pow(),掉精度

#include<bits/stdc++.h>
 
using namespace std;
 
const int MAXN = 100000 + 10;
int a[MAXN];long long  mm = 1e9 + 7;
 
long long  quick(int p)
{
    long long int ans = 1;
    long long int q = 2, t = p;
    while(p)
    {
        if(p & 1)               //二进制下最后一位为1即为真,0即为否
            ans = ans * q;
        q=q*q;
        p>>=1;
    }
    return ans;
}
int main()
{
    int T,n;
    cin >> T;
    while(T -- )
    {
        cin >> n;
        long long  ans = 0;
        for(int i = 1; i <= n ; i ++)
            cin >> a[i];
        for(int i = 1 ; i <= 31; i ++)
        {
            long long l = 0 , r = 0;
            for(int j = 1; j <= n ; j++)
            {
                if(a[j]&1)
                    l ++;
                else
                    r ++;
                a[j] >>= 1;
            }
            //cout << l << " " << r << " " <<pow(2,i) << endl;
            int t = quick(i);
            long long  temp = l * r * t;
            temp %= mm;
            ans += temp;
            ans %= mm;
 
        }
        ans %= mm;
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ant_e_zz/article/details/80378139