大家画图自己领会,反正就是位运算.

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N],last[2],c1,c2,n;
double ans_xor=0.0,ans_and=0.0,ans_or=0.0;

void solve(int k)
{
    c1=0,c2=0;
    double pos=(double)(1<<k)/n/n;//价值
    last[0]=last[1]=0;
    for(int i=1;i<=n;i++)
    {
        int v=(a[i]>>k&1);
        if(v)
        {
            ans_xor+=pos;
            ans_and+=pos;
            ans_or+=pos;
        }
        if(v)
        {
            ans_and+=pos*(i-last[0]-1)*2.0;
            ans_or+=pos*(i-1)*2.0;
            ans_xor+=(c1)*pos*2.0;
        }
        else
        {
            ans_or+=pos*(last[1])*2.0;
            ans_xor+=(c2)*pos*2.0;
        }
        c1++;
        if(v) swap(c1,c2);
        last[v]=i;
    }
}

int main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    //cout<<(1<<30)<<endl;
    for(int i=0;i<=30;i++) solve(i);
    printf("%.3lf %.3lf %.3lf\n",ans_xor,ans_and,ans_or);
    return 0;
}