Rainbow signal CH3801

Topic Link

That Italy: an arbitrary integer n seeking to take one section, xor operation, and, together with or or, xor find the expected value, and the expected value, or expectations.

Thinking: left section taken point l, the right end is r, when r == l, selected probability 1 / n / n, and R & lt! Time = l, selected probability 2 / n / n.

Since then binary operation, so enumerate every bit integer. Three operation three cases:

1and: Consider first enumerate a right point r, consider and nature, so consider find the location of a 0 before the first appearance of the last [0], if this bit is also 1, then the left end point can be taken [last [0 ] + 1, r-1] .

2or: still consider enumerate the right point r, find the location before a 1 appeared last [1], if this bit is 1, then you can take the left point [1, r-1], if this bit is not 0, so the left point may take [1, last [1]] .

3xor: very detailed guide on advanced algorithms competition.

   First still enumerate the right point r, because x nature or, so consider find all point 1, followed by black and white coloring these points, it will be left endpoint can take all the white segment, and finally recursive look.

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#include<vector>
#include<string>
#include<set>
#define ll long long
using namespace std;
const int N=1e6+10;
int n;
int a[N],b[N];
double ansa,anso,ansx;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    for(int k=0;k<31;k++)
    {
        int last[2]={0,0},c1=0,c2=0;
        for(int i=1;i<=n;i++)
        {
            b[i]=((a[i]>>k)&1);
            if(b[i])
            {
                ansa+=(1<<k)*1.0/n/n;
                anso+=(1<<k)*1.0/n/n;
                ansx+=(1<<k)*1.0/n/n;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(!b[i])
            {
                anso+=(1<<k)*2.0/n/n*last[1];
            }
            else
            {
                ansa+=(1<<k)*2.0/n/n*(i-1-last[0]);
                anso+=(1<<k)*2.0/n/n*(i-1);
            }
            ansx+=(1<<k)*2.0/n/n*(b[i]?c1:c2);
            c1++;
            if(b[i])
            swap(c1,c2);
            last[b[i]]=i;
        }
    }
    printf("%.3lf %.3lf %.3lf\n",ansx,ansa,anso);
}

 

Guess you like

Origin www.cnblogs.com/2462478392Lee/p/11332960.html
Recommended