CodeForces Div1: 995 D. Game(数学期望)

Allen and Bessie are playing a simple number game. They both know a function f:{0,1}nRf:{0,1}n→R, i. e. the function takes nn binary arguments and returns a real value. At the start of the game, the variables x1,x2,,xnx1,x2,…,xn are all set to 1−1. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an ii such that xi=1xi=−1 and either setting xi0xi→0 or xi1xi→1.

After nn rounds all variables are set, and the game value resolves to f(x1,x2,,xn)f(x1,x2,…,xn). Allen wants to maximize the game value, and Bessie wants to minimize it.

Your goal is to help Allen and Bessie find the expected game value! They will play r+1r+1 times though, so between each game, exactly one value of ff changes. In other words, between rounds ii and i+1i+1 for 1ir1≤i≤r, f(z1,,zn)gif(z1,…,zn)→gi for some (z1,,zn){0,1}n(z1,…,zn)∈{0,1}n. You are to find the expected game value in the beginning and after each change.

Input

The first line contains two integers nn and rr (1n181≤n≤18, 0r2180≤r≤218).

The next line contains 2n2n integers c0,c1,,c2n1c0,c1,…,c2n−1 (0ci1090≤ci≤109), denoting the initial values of ff. More specifically, f(x0,x1,,xn1)=cxf(x0,x1,…,xn−1)=cx, if x=xn1x0¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯x=xn−1…x0¯ in binary.

Each of the next rr lines contains two integers zz and gg (0z2n10≤z≤2n−1, 0g1090≤g≤109). If z=zn1z0¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯z=zn−1…z0¯ in binary, then this means to set f(z0,,zn1)gf(z0,…,zn−1)→g.

Output

Print r+1r+1 lines, the ii-th of which denotes the value of the game ff during the ii-th round. Your answer must have absolute or relative error within 10610−6.

Formally, let your answer be aa, and the jury's answer be bb. Your answer is considered correct if |ab|max(1,|b|)106|a−b|max(1,|b|)≤10−6.

Examples

Input
2 2
0 1 2 3
2 5
0 4
Output
1.500000
2.250000
3.250000
Input
1 0
2 3
Output
2.500000
Input
2 0
1 1 1 1
Output
1.000000

Note

Consider the second test case. If Allen goes first, he will set x11x1→1, so the final value will be 33. If Bessie goes first, then she will set x10x1→0 so the final value will be 22. Thus the answer is 2.52.5.

In the third test case, the game value will always be 11 regardless of Allen and Bessie's play.

题意: 给定N, 以及0到(1<<N)-1对应的值Wi,  现在两个人在玩游戏, 对于(1<<N)位, 开始每一位都是-1,  每一轮随机一个人把某个是-1的数 改为0或者1,最后得到二进制下对应的值W, A想要W越大,  B想要W越小,  问每次更改一个对应的值, 期望W是多少.

思路: 因为二人希望最后的结果(大小的倾向)相反, 所以对于每一位, 肯定是被改为0或者1的几率相同, 所以每个二进制都是有可能被选的, 所以结果就算平均数.

(反证法易证.

#include<bits/stdc++.h>
using namespace std;
double a[1000010],x,ans;
int main()
{
    int N,R,W,i,j;
    scanf("%d%d",&N,&R);W=1<<N;
    for(i=0;i<W;i++) scanf("%lf",&a[i]),ans+=a[i];
    printf("%.8lf
",ans/W);
    for(i=1;i<=R;i++){
        scanf("%d%lf",&j,&x);
        ans-=a[j]; a[j]=x; ans+=a[j];
        printf("%.8lf
",ans/W);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hua-dong/p/9224991.html