其他分享
首页 > 其他分享> > Codeforces1466 E. Apollo versus Pan(位运算)

Codeforces1466 E. Apollo versus Pan(位运算)

作者:互联网

题意:

给定长度为n的非负序列x,要求计算:
在这里插入图片描述
答案对1e9+7取模。

数据范围:n<=5e5,0<=x[i]<=260

解法:

∑ i = 1 n ∑ j = 1 n ∑ k = 1 n ( x i & x j ) ∗ ( x j ∣ x k ) = ∑ i = 1 n ∑ j = 1 n ( x i & x j ) ∗ ∑ k = 1 n ( x j ∣ x k ) = ∑ j = 1 n ( ∑ i = 1 n ( x i & x j ) ∗ ∑ k = 1 n ( x j ∣ x k ) ) = ∑ j = 1 n f ( j ) ∗ g ( j ) 按 位 计 算 贡 献 即 可 \sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n(x_{i}\&x_{j})*(x_{j}|x_{k})\\ =\sum_{i=1}^n\sum_{j=1}^n(x_{i}\&x_{j})*\sum_{k=1}^n(x_{j}|x_{k})\\ =\sum_{j=1}^n(\sum_{i=1}^n(x_{i}\&x_{j})*\sum_{k=1}^n(x_{j}|x_{k}))\\ =\sum_{j=1}^nf(j)*g(j)\\ 按位计算贡献即可 i=1∑n​j=1∑n​k=1∑n​(xi​&xj​)∗(xj​∣xk​)=i=1∑n​j=1∑n​(xi​&xj​)∗k=1∑n​(xj​∣xk​)=j=1∑n​(i=1∑n​(xi​&xj​)∗k=1∑n​(xj​∣xk​))=j=1∑n​f(j)∗g(j)按位计算贡献即可

code:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=2e6+5;
const int mod=1e9+7;
int a[maxm];
int cnt[66];
int n;
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int T;cin>>T;
    while(T--){
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        int ans=0;
        for(int j=0;j<60;j++)cnt[j]=0;
        for(int i=1;i<=n;i++)for(int j=0;j<60;j++)cnt[j]+=(a[i]>>j&1);
        for(int j=1;j<=n;j++){
            int f=0,g=0;
            for(int p=0;p<60;p++){
                if(a[j]>>p&1){
                    f+=(1ll<<p)%mod*cnt[p]%mod;
                    g+=(1ll<<p)%mod*n%mod;
                    f%=mod,g%=mod;
                }else{
                    g+=(1ll<<p)%mod*cnt[p]%mod;
                    g%=mod;
                }
            }
            ans+=f*g%mod;
            ans%=mod;
        }
        cout<<ans<<endl;
    }
    return 0;
}

标签:versus,xj,Codeforces1466,xi,xk,int,sum,cin,Apollo
来源: https://blog.csdn.net/weixin_44178736/article/details/112058018