LOJ2541 PKUWC2018猎人杀(概率期望+容斥原理+生成函数+分治NTT)

  考虑容斥,枚举一个子集S在1号猎人之后死。显然这个概率是w1/(Σwi+w1) (i∈S)。于是我们统计出各种子集和的系数即可,造出一堆形如(-xwi+1)的生成函数,分治NTT卷起来就可以了。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 100010
#define P 998244353
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int n,a[N],s[N],r[N*3],inv[N*3],f[N*3],t,ans;
int ksm(int a,int k)
{
    int s=1;
    for (;k;k>>=1,a=1ll*a*a%P) if (k&1) s=1ll*s*a%P;
    return s; 
}
void DFT(int *a,int n,int g)
{
    for (int i=0;i<n;i++) r[i]=(r[i>>1]>>1)|(i&1)*(n>>1);
    for (int i=0;i<n;i++) if (i<r[i]) swap(a[i],a[r[i]]);
    for (int i=2;i<=n;i<<=1)
    {
        int wn=ksm(g,(P-1)/i);
        for (int j=0;j<n;j+=i)
        {
            int w=1;
            for (int k=j;k<j+(i>>1);k++,w=1ll*w*wn%P)
            {
                int x=a[k],y=1ll*w*a[k+(i>>1)]%P;
                a[k]=(x+y)%P,a[k+(i>>1)]=(x-y+P)%P;
            }
        }
    }
}
void mul(int *a,int *b,int n)
{
    DFT(a,n,3),DFT(b,n,3);
    for (int i=0;i<n;i++) a[i]=1ll*a[i]*b[i]%P;
    DFT(a,n,inv[3]);
    for (int i=0;i<n;i++) a[i]=1ll*a[i]*inv[n]%P;
}
void solve(int l,int r,int *f,int n)
{
    if (l==r) {f[0]=1,f[a[l]]=P-1;return;}
    int a[n]={0},mid=l;
    for (int i=l;i<=r;i++) if (s[i]-s[l-1]>s[r]-s[i]) {mid=i;break;}
    if (mid==r) mid--;
    int t1=1;while (t1<=(s[mid]-s[l-1]<<1)) t1<<=1;
    solve(l,mid,f,t1);
    t1=1;while (t1<=(s[r]-s[mid]<<1)) t1<<=1;
    solve(mid+1,r,a,t1);
    mul(f,a,n);
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("loj2541.in","r",stdin);
    freopen("loj2541.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    n=read();
    for (int i=1;i<=n;i++) s[i]=s[i-1]+(a[i]=read());
    t=1;while (t<=(s[n]<<1)) t<<=1;
    inv[1]=1;for (int i=2;i<N*3;i++) inv[i]=P-1ll*(P/i)*inv[P%i]%P;
    solve(2,n,f,t);
    for (int i=0;i<=s[n];i++)
    ans=(ans+1ll*a[1]*inv[i+a[1]]%P*f[i])%P;
    cout<<ans;
    return 0;
}

 

posted @ 2018-12-20 13:15  Gloid  阅读(199)  评论(0编辑  收藏  举报