Codeforces1466 E. Apollo versus Pan(位运算)

题意:

给定长度为n的非负序列x,要求计算:
Codeforces1466 E. Apollo versus Pan(位运算)
答案对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;
}

上一篇:Docker部署Apollo环境


下一篇:Apollo自动驾驶实践——第0讲:导论