[B] JZOJ NOIP2019 analog 2019.9.4

Description

Here Insert Picture Description

Input

Here Insert Picture Description

Output

Here Insert Picture Description

Sample Input

3
2 3 3

Sample Output

202020207

Data Constraint

Here Insert Picture Description

Thinking

Consider a certain position, assuming that there is an element not present, then the probability of each element is operated. If only concerned with two elements and can be found other elements operate when there is no effect on them, and they are operating two probabilities are equal. So the problem is equivalent to one of only two elements of the original problem.
So it is independent between the elements! Dp calculated using the contribution of each element of the answer, the summation.

Time complexity: O (A 2 + n-)

optimization:

The task just abstracted from (a1, ai) began to walk to (0,0), it can be O (1) Statistics answer

Code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=(5e5+77)*3,mod=323232323;
int f[N],g[N],fac[N],unfac[N],a[N],ans,mx,n;
ll power(ll x,ll t)
{
	ll b=1;
	while(t)
	{
		if(t&1) b=b*x%mod; x=x*x%mod; t>>=1;
	}
	return b;
}
void init()
{
	fac[0]=1;
	for(int i=1; i<=mx<<1; i++) fac[i]=1ll*fac[i-1]*i%mod;
	unfac[mx<<1]=(int)power(fac[mx<<1],mod-2);
	for(int i=(mx<<1)-1; i>=0; i--)
		unfac[i]=1ll*unfac[i+1]*(i+1)%mod; 
}
ll C(ll x,ll y)
{
	if(x<y) return 0;
	return 1ll*fac[x]*unfac[y]%mod*unfac[x-y]%mod;
}
int main()
{
	freopen("b.in","r",stdin); freopen("b.out","w",stdout);
	scanf("%d",&n);
	for(int i=1; i<=n; i++) scanf("%d",&a[i]);
	mx=500000;
	init();
	for(int i=0; i<=mx; i++)
    {
        f[i]=1ll*power(unfac[2],a[1]+i)*C(a[1]+i-1,i)%mod;
        g[i]=1ll*f[i]*i%mod;
        if(i) f[i]=(f[i]+f[i-1])%mod,g[i]=(g[i]+g[i-1])%mod;
    }
    ans=a[1];
    for(int i=2; i<=n; i++) ans=(ans+g[a[i]]+1ll*(mod+1-f[a[i]])*a[i])%mod;
    printf("%d\n",ans);
} 
Published 703 original articles · won praise 392 · Views 140,000 +

Guess you like

Origin blog.csdn.net/Eric1561759334/article/details/100586564