[JZOJ6340] [B] NOIP2019 analog 2019.9.4

topic

Subject to the effect

You nonnegative integer number of columns \ (A \) , the probability of selecting each other greater than zero \ (a_i \) , so minus \ (1 \) .
Q \ (a_1 \) is reduced to \ (0 \) how many times the operating time expectations through.


Reflections on History

For the practice of this violence problem, it is clear the state can compress it ......
then I suddenly realized that, in fact, we will be subject translated into the following models:
There \ (n \) colors, the \ (i \) colors balls there \ (a_i \) months. So the subject becomes a question of repeating elements are arranged.
First \ (2 \) to the \ (n-\) arranged seek out, and then consider the \ (1 \) random insertion.
The last enumeration \ (1 \) where they appear, then the program number is inserted in front of a number of combinations is calculated.
After playing out this practice has not been transferred out, I do not know math skills or not this approach always been wrong ......
Of course, even is right, it will not be out of practice.


Correct

The desired linear resistance, can be transformed into the title: for each \ (I>. 1 \) , \ (A_1 \) reduced \ (0 \) when \ (a_i \) desired to take a number. Then each \ (i \) of these things add up, plus \ (A_1 \) , is the answer.
So we only need to consider this issue.
Next show is the most action: We only consider \ (a_1 \) and \ (a_i \) , the other is lost no effect on them. They have an equal probability of being minus \ (1 \) , this probability can be considered as \ (\ FRAC 1} {2} {\) . Thus corresponds title \ (n = 2 \) conditions.
Now consider a plane, the coordinates of the beginning of \ ((A_1, a_i) \) , each time a random walk or below grid to the left.
If you reach the boundary \ ((0, the y-) \) , then there will be \ (a_i-y \) contributions; if reaches the boundary \ ((the X-, 0) \) , then there will be \ (a_i \) contribution.
For boundary \ ((0, y) \ ), Go down a set \ (J \) step, the probability \ (\ {C_ {A_1 FRAC + J-J. 1} ^ {}} + {2 ^ {A_1} J} \) .
For boundary \ ((the X-, 0) \) , since their contributions are the same, it might be above and with \ (1 \) to lose, it is a probability.
Then can list a formula ......
after listed easy to find, \ ((A_1, a_i +. 1) \) answers may \ (O (1) \) from \ ((a_i, a_i) \ ) Transfer come.


Code

using namespace std;
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>
#define N 500010
#define A 500010
#define mo 323232323
inline int input(){
    char ch=getchar();
    while (ch<'0' || '9'<ch)
        ch=getchar();
    int x=0;
    do{
        x=x*10+ch-'0';
        ch=getchar();
    }
    while ('0'<=ch && ch<='9');
    return x;
}
int n,a[N];
int fac[N+A],invf[N+A],inv2[N+A];
int ans1[A*3],ans2[A*3],ans[A*3];
inline int inv(int x){
    int res=1;
    for (int y=mo-2;y;y>>=1,x=(long long)x*x%mo)
        if (y&1)
            res=(long long)res*x%mo;
    return res;
}
inline int C(int m,int n){return (long long)fac[m]*invf[n]%mo*invf[m-n]%mo;}
int main(){
    freopen("b.in","r",stdin);
    freopen("b.out","w",stdout);
    n=input();
    for (int i=1;i<=n;++i)
        a[i]=input();
    fac[0]=1,inv2[0]=1;
    for (int i=1;i<=A*2;++i){
        fac[i]=(long long)fac[i-1]*i%mo;
        inv2[i]=(long long)inv2[i-1]*2%mo;
    }
    for (int i=0;i<=A*2;++i){
        invf[i]=inv(fac[i]);
        inv2[i]=inv(inv2[i]);
    }
    ans1[0]=ans2[0]=0;
    for (int i=1;i<=A;++i){
        ans1[i]=(ans1[i-1]+(long long)C(a[1]+i-2,i-1)*(i-1)%mo*inv2[a[1]+i-1]%mo)%mo;
        ans2[i]=(ans2[i-1]+(long long)C(a[1]+i-2,i-1)*inv2[a[1]+i-1]%mo)%mo;
        ans[i]=(ans1[i]+(long long)i*(1-ans2[i]+mo)%mo)%mo;
    }
    long long sum=a[1];
    for (int i=2;i<=n;++i)
        sum+=ans[a[i]];
    printf("%lld\n",sum%mo);
    return 0;
}

to sum up

Desired linearity is really wonderful ah ......

Guess you like

Origin www.cnblogs.com/jz-597/p/11483219.html