HDU - 6314 Matrix(广义容斥原理)

时间:2021-12-20 07:30:08

http://acm.hdu.edu.cn/showproblem.php?pid=6314

题意

对于n*m的方格,每个格子只能涂两种颜色,问至少有A列和B行都为黑色的方案数是多少。

分析

参考https://blog.csdn.net/IcePrincess_1968/article/details/81255138

重点在于计算容斥系数。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = + ;
const int maxm = + ;
const int mod = ; int c[maxn][maxn],pw[maxn*maxn],fa[maxn],fb[maxn]; inline int add(int x){
if(x>=mod) x-=mod;
return x;
}
inline int sub(int x){
if(x<) x+=mod;
return x;
}
inline void init() {
c[][]=;
for(int i=;i<maxn;i++){
c[i][]=c[i][i]=;
for(int j=;j<i;j++) c[i][j]=add(c[i-][j]+c[i-][j-]);
}
pw[]=;
for(int i=;i<maxn*maxn;i++){
pw[i]=add(pw[i-]+pw[i-]);
}
} int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n,m,A,B;
init();
while(~scanf("%d%d%d%d",&n,&m,&A,&B)){
fa[A]=;
for(int i=A+;i<=n;i++){
fa[i]=;
for(int j=A;j<i;j++){
fa[i]=sub(fa[i]-1ll*c[i-][j-]*fa[j]%mod);
}
}
fb[B]=;
for(int i=B+;i<=m;i++){
fb[i]=;
for(int j=B;j<i;j++){
fb[i]=sub(fb[i]-1ll*c[i-][j-]*fb[j]%mod);
}
}
int ans=;
for(int i=A;i<=n;i++){
for(int j=B;j<=m;j++){
ans=add(ans+1ll*fa[i]*fb[j]%mod*c[n][i]%mod*c[m][j]%mod*pw[(n-i)*(m-j)]%mod);
}
}
printf("%d\n",ans);
}
return ;
}