T1 小a的强迫症
dp + 组合数学
code:
//By Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#define LL long long
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
#define R(i,a,b) for(register int i=(b);i>=(a);i--)
using namespace std;
LL rd() {
LL x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f; c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
const int MOD=998244353;
const int N=100010,M=500010;
int n;
int da[N];
LL fac[M],inv[M];
LL qpow(LL a,LL b) {
LL ret=1;
while(b) {
if(b&1) ret=ret*a%MOD;
a=a*a%MOD;
b>>=1;
}
return ret;
}
void init() {
fac[1]=1;
F(i,2,M) fac[i]=fac[i-1]*i%MOD;
inv[M]=qpow(fac[M],MOD-2);
R(i,-1,M-1) inv[i]=(inv[i+1]*(i+1))%MOD;
}
LL C(int x,int y) {
return fac[x]*inv[y]%MOD*inv[x-y]%MOD;
}
int main() {
freopen("qiang.in","r",stdin);
freopen("qiang.out","w",stdout);
n=rd();
F(i,1,n) da[i]=rd();
init();
LL ans=1,sum=da[1];
F(i,2,n) ans=ans*C(sum+da[i]-1,da[i]-1)%MOD,sum+=da[i];
printf("%lld",ans);
return 0;
}
T2 数格子
矩阵快速幂 优化 状压dp
code:
//By Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#define LL long long
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
#define R(i,a,b) for(register int i=(b);i>=(a);i--)
using namespace std;
LL rd() {
LL x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f; c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
const int MOD=998244353;
const int N=100010,M=500010;
int n;
int da[N];
LL fac[M],inv[M];
LL qpow(LL a,LL b) {
LL ret=1;
while(b) {
if(b&1) ret=ret*a%MOD;
a=a*a%MOD;
b>>=1;
}
return ret;
}
void init() {
fac[1]=1;
F(i,2,M) fac[i]=fac[i-1]*i%MOD;
inv[M]=qpow(fac[M],MOD-2);
R(i,-1,M-1) inv[i]=(inv[i+1]*(i+1))%MOD;
}
LL C(int x,int y) {
return fac[x]*inv[y]%MOD*inv[x-y]%MOD;
}
int main() {
freopen("qiang.in","r",stdin);
freopen("qiang.out","w",stdout);
n=rd();
F(i,1,n) da[i]=rd();
init();
LL ans=1,sum=da[1];
F(i,2,n) ans=ans*C(sum+da[i]-1,da[i]-1)%MOD,sum+=da[i];
printf("%lld",ans);
return 0;
}