标签:wal temp 答案 type man 矩阵 protect 枚举 from
Samwell Tarly is learning to draw a magical matrix to protect himself from the White Walkers.
the magical matrix is a matrix with n rows and m columns, and every single block should be painted either black or white.
Sam wants to know how many ways to paint the matrix, satisfied that the final matrix has at least A rows, B columns was painted completely black. Cause the answer might be too big, you only need to output it modulo 998244353.
There might be multiple test cases, no more than \(5\). You need to read till the end of input.
For each test case, a line containing four integers \(n,m,A,B. 1≤n,m,A,B≤3000.\)
For each test case, output a line containing the answer modulo 998244353.
思路:
容斥,先推式子
\(\mathfrak{Talk\ is\ cheap,show\ you\ the\ code.}\)
#include<map>
#include<cmath>
#include<stack>
#include<deque>
#include<queue>
#include<cstdio>
#include<vector>
#include<climits>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
# define Type template<typename T>
# define ll long long
# define read read1<ll>()
Type T read1(){
T t=0;char k;
bool v=0;
do (k=getchar())==‘-‘&&(v=1);while(‘0‘>k||k>‘9‘);
while(‘0‘<=k&&k<=‘9‘)t=(t<<3)+(t<<1)+(k^‘0‘),k=getchar();
return v?-t:t;
}
ll qkpow(ll n,ll x,ll mo){
if(!x)return 1;
ll t=qkpow(n,x>>1,mo);
t=t*t%mo;
if(x&1)t=t*n%mo;
return t;
}
# define mod 998244353ll
# define I 499122177ll
# define N 3000
ll fac[N|3],inv[N|3];
ll Ch(ll m,ll n){
if(m<n)return 0;
return fac[m]*inv[m-n]%mod*inv[n]%mod;
}
void init(){
fac[0]=1;
for(int i=1;i<=N;++i)
fac[i]=fac[i-1]*i%mod;
inv[N]=qkpow(fac[N],mod-2,mod);
for(int i=N;i;--i)
inv[i-1]=inv[i]*i%mod;
}
ll invi(ll x){return qkpow(x,mod-2,mod);}
ll f[6005],f1[6005];
int main(){
init();
int n,m,A,B;
while(~scanf("%d %d %d %d",&n,&m,&A,&B)){
ll ans=0;
memset(f,0,sizeof(f));
memset(f1,0,sizeof(f1));
for(int i=A;i<=n;++i)
for(int k=0;k<=n-i;++k)
f[i+k]=(f[i+k]+1ll*(k&1?-1:1)*Ch(n-i,k)*Ch(n,i))%mod;
for(int i=B;i<=m;++i)
for(int k=0;k<=m-i;++k)
f1[i+k]=(f1[i+k]+1ll*(k&1?-1:1)*Ch(m-i,k)*Ch(m,i))%mod;
for(int i=A;i<=n;++i)
for(int j=B,v=qkpow(2,(n-i)*(m-j),mod),x=invi(qkpow(2,n-i,mod));j<=m;v=1ll*x*v%mod,++j)
ans=(ans+f[i]*f1[j]%mod*v)%mod;
printf("%lld\n",(ans+mod)%mod);
}
return 0;
}
标签:wal temp 答案 type man 矩阵 protect 枚举 from
原文地址:https://www.cnblogs.com/SYDevil/p/13626097.html