题意

有n个敌人,m种攻击,第i种攻击会攻击$a_i$次,每次攻击会对敌人总共造成$1\dots b_i$点伤害,

最后每个敌人都至少要受到1点伤害,求不同攻击方案数。

两种方案不同当且仅当某次攻击的总伤害不同或某个敌人受到的伤害不同。

$n\cdot m\le 100000$

题解

设攻击的总伤害为$s$,那么敌人受到的伤害不同方案数为$\binom{s-1}{n-1}$。

相当于除了第一点伤害以外选出$n-1$点伤害的方案数。

那么对于每种攻击计算这一次攻击中选出$k$点伤害的方案数。

这个方案数$=\sum_{1\le i\le b_i}\binom{i}{k}=\sum_{0\le i\le b_i}\binom{i}{k}-[k=0]=\binom{b_i+1}{k+1}-[k=0]$。

$b_i$很大,但是$k$不大,可以递推。

多项式快速幂/exp+ln多项式幂次然后卷积起来。

/*
Author: QAQ Automaton
Lang: C++
Prog: E.cpp
Mail: lk@qaq-am.com
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define int long long
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;}
template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T>T mmin(T a,T b){return a<b?a:b;}
template<class T>T mmax(T a,T b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
namespace io {
    const int SIZE = (1 << 21) + 1;
    char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
    // getchar
    #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
    // print the remaining part
    inline void flush () {
        fwrite (obuf, 1, oS - obuf, stdout);
        oS = obuf;
    }
    // putchar
    inline void putc (char x) {
        *oS ++ = x;
        if (oS == oT) flush ();
    }
    // input a signed integer
    inline bool read (signed &x) {
        for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
        for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
        return 1;
    }

    inline bool read (long long &x) {
        for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
        for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
        return 1;
    }
    inline bool read (char &x) {
        x=gc();
        return x!=EOF;
    }
    inline bool read(char *x){
        while((*x=gc())=='\n' || *x==' '||*x=='\r')if(*x==EOF)return 0;
        while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc();
        *x=0;
        return 1;
    }
    template<typename A,typename ...B>
    inline bool read(A &x,B &...y){
        return read(x)&&read(y...);
    }
    // print a signed integer
    inline bool write (signed x) {
        if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
        while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
        while (qr) putc (qu[qr --]);
        return 0;
    }

    inline bool write (long long x) {
        if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
        while (x) qu[++ qr] = x % 10 + '0',  x /= 10;
        while (qr) putc (qu[qr --]);
        return 0;
    }
    inline bool write (char x) {
        putc(x);
        return 0;
    }
    inline bool write(const char *x){
        while(*x){putc(*x);++x;}
        return 0;
    }
    inline bool write(char *x){
        while(*x){putc(*x);++x;}
        return 0;
    }
    template<typename A,typename ...B>
    inline bool write(A x,B ...y){
        return write(x)||write(y...);
    }
    //no need to call flush at the end manually!
    struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
int inf;
struct _init_{
    _init_(){
        memset(&inf,0x3f,sizeof(inf));
    }
};
_init_ ___INIT__;
const int p=998244353;
int fac[200005],inv[200005],invf[200005];
int a[200005],b[200005];
int fpm(int a,int b){
    int c=1;
    for(;b;b>>=1,a=a*a%p)if(b&1)c=c*a%p;
    return c;
}
namespace Polynomial{
    const ll p=998244353,g_=3;
    ll rev[266667];
    ll fpm(ll a,ll b){
        ll c=1;
        while(b){
            if(b&1)c=c*a%p;
            a=a*a%p;
            b>>=1;
        }
        return c;
    }
    void Rev(ll n,ll *f){
        for(ll i=0;i<=n&&i<n-i;++i)swap(f[i],f[n-i]);
    }
    void NTT(ll *f,ll n,ll flag){
        for(ll i=1;i<n;++i){
            rev[i]=(rev[i>>1]>>1)|((i&1)*(n>>1));
            if(i<rev[i])swap(f[i],f[rev[i]]);
        }
        for(ll i=1;i<n;i<<=1){
            ll ww=fpm(g_,flag*((p-1)/(i+i))+p-1);
            for(ll j=0;j<n;j+=i+i){
                ll w=1,u,v; 
                for(ll k=0;k<i;++k){
                    u=f[j+k];v=f[j+k+i]*w%p;
                    f[j+k]=(u+v)%p;
                    f[j+k+i]=(u-v+p)%p;
                    w=w*ww%p;
                }
            }
        }
        if(!~flag){
            ll in=fpm(n,p-2);
            for(ll i=0;i<n;++i)f[i]=f[i]*in%p;
        }
    }
    ll f1[266667],g1[266667];
    void Mul(ll n,ll *f,ll m,ll *g,ll *h){//卷积
        ll N=1;
        while(N<=n+m)N<<=1;
        for(ll i=0;i<N;++i){f1[i]=f[i];if(i>n)f[i]=0;}
        for(ll i=0;i<N;++i){g1[i]=g[i];if(i>m)g[i]=0;}
        NTT(f,N,1);
        if(f!=g)NTT(g,N,1);
        for(ll i=0;i<N;++i)h[i]=f[i]*g[i]%p;
        NTT(h,N,-1);
        if(f!=h){for(ll i=0;i<N;++i)f[i]=f1[i];}
        if(g!=h){for(ll i=0;i<N;++i)g[i]=g1[i];}
    }
    /*
1: Inv
2: Div ln
3. Sqrt exp
4. Sqrt Pow
*/
    ll h1[266667],h2[266667],h3[266667],h4[266667];
    void Inv(ll n,ll *f,ll *g){//求逆
        if(n==1){
            g[0]=fpm(*f,p-2);
            g[1]=g[2]=g[3]=0;
            return;
        }
        else{
            ll m=(n+1)>>1;
            Inv(m,f,g);
            ll N=1;
            while(N<=n+m+m-3)N<<=1;
            for(ll i=0;i<N;++i){f1[i]=f[i];if(i>=n)f[i]=0;if(i>=m)g[i]=0;}
            NTT(f,N,1);
            NTT(g,N,1);
            for(ll i=0;i<N;++i)g[i]=(g[i]+g[i]-g[i]*g[i]%p*f[i]%p+p)%p;
            NTT(g,N,-1);
            for(ll i=0;i<N;++i)f[i]=f1[i];
            for(ll i=n;i<N;++i)g[i]=0;
        }
    }
    void Div(ll n,ll *f,ll m,ll *g,ll *q,ll *r){//除法
        Rev(n,f);
        Rev(m,g);
        Inv(n-m+1,g,h2);
        for(ll i=n-m+1;i<=n+n-m-m+2;++i)q[i]=0;
        Rev(n,f);
        Rev(m,g); 
        Rev(n-m,q);
        Mul(m,g,n-m,q,r);
        for(ll i=0;i<=n;++i){r[i]=(f[i]-r[i]+p)%p;}
    }
    void Sqrt(ll n,ll *f,ll *g){//开根
        if(n==1){
            g[0]=(ll)(sqrt(f[0])+0.5);
            g[1]=0;
        }
        else{
            ll m;
            Sqrt(m=(n+1)>>1,f,g);
            Mul(m-1,g,m-1,g,h3);
            if(n-1>m+m-2)h3[n-1]=0;
            for(ll i=0;i<n;++i)h3[i]=(h3[i]+f[i])%p;
            for(ll i=0;i<m;++i)g[i]=g[i]*2%p;
            Inv(n,g,h4);
            Mul(n-1,h4,n-1,h3,g);
            for(ll i=n;i<n+n;++i)g[i]=0;
        }
    }
    ll inv[266667];
    void Integ(ll n,ll *f,ll *g){//积分
        inv[1]=1;
        for(ll i=2;i<=n+1;++i)inv[i]=(p-p/i)*inv[p%i]%p;
        for(ll i=n;~i;--i)g[i+1]=f[i]*inv[i+1]%p;
        g[0]=0;
    }
    void Deriv(ll n,ll *f,ll *g){//求导
        for(ll i=1;i<=n;++i)g[i-1]=f[i]*i%p;
        g[n]=0;
    }
    void ln(ll n,ll *f,ll *g){//对数
        Inv(n,f,h2);
        Deriv(n-1,f,g);
        Mul(n-1,h2,n-2,g,g);
        for(ll i=n-1;i<=n+n-3;++i)g[i]=0;
        Integ(n-2,g,g);
    }
    void exp(ll n,ll *f,ll *g){//指数
        if(n==1){
            g[0]=1;
            g[1]=0;
        }
        else{
            exp((n+1)>>1,f,g);
            ln(n,g,h3);
            for(int i=0;i<n;++i){
                h3[i]=(f[i]-h3[i]+p)%p;
            }
            h3[0]=(h3[0]+1)%p;
            Mul(n-1,h3,(n-1)>>1,g,g);
            for(int i=n;i<n+n;++i)g[i]=0;
        }
    }
    void Pow(ll n,ll *f,ll k,ll *g){//幂次
        for(int i=0;i<=n;++i)g[i]=i==0;
        for(;k;k>>=1){
            if(k&1)Mul(n,g,n,f,g);    
            Mul(n,f,n,f,f);
        }
    }
}
int ans[300005],f[300005],g[300005];
void Calc(int a,int n,int *f){
    f[0]=a+1;
    for(int i=1;i<=n;++i)f[i]=f[i-1]*(a-i+1)%p*inv[i+1]%p;
}
signed main(){
#ifdef QAQAutoMaton 
    freopen("E.in","r",stdin);
    freopen("E.out","w",stdout);
#endif
    int n,m;
    read(n,m);
    inv[1]=1;
    for(int i=2;i<=200000;++i){
        inv[i]=(p-p/i)*inv[p%i]%p;
    }

    for(int i=1;i<=m;++i)read(a[i],b[i]);
    Calc(b[1]-1,n,ans);
    --a[1];
    for(int i=1;i<=m;++i)if(a[i]){
        Calc(b[i],n,f);
        f[0]=(f[0]+p-1)%p;
        Polynomial::Pow(n-1,f,a[i],g);
        Polynomial::Mul(n-1,ans,n-1,g,ans);
        for(int j=0;j<=n+n;++j)f[i]=g[i]=0;
    }
    write(ans[n-1],'\n');
    return 0;
}

标签: 数学

添加新评论