An n × n square matrix is special, if:
- it is binary, that is, each cell contains either a 0, or a 1;
- the number of ones in each row and column equals 2.
You are given n and the first m rows of the matrix. Print the number of special n × n matrices, such that the first m rows coincide with the given ones.
As the required value can be rather large, print the remainder after dividing the value by the given number mod.
The first line of the input contains three integers n, m, mod (2 ≤ n ≤ 500, 0 ≤ m ≤ n, 2 ≤ mod ≤ 109). Then m lines follow, each of them contains n characters — the first rows of the required special matrices. Each of these lines contains exactly two characters '1', the rest characters are '0'. Each column of the given m × n table contains at most two numbers one.
Print the remainder after dividing the required value by number mod.
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
int x,y;
}dl[600000];
int l,r,a[510],c[510][510],q[510][510];///f[i,j]i个0空位,j个1空位。
long long mod,f[510][510];
void ditui(int k)
{
q[dl[k].x][dl[k].y]=0;
if (dl[k].x>0&&dl[k].y>0) {
if (!q[dl[k].x][dl[k].y-1]) {
r++;dl[r].x=dl[k].x;dl[r].y=dl[k].y-1;
f[dl[k].x][dl[k].y-1]=(f[dl[k].x][dl[k].y]*dl[k].x*dl[k].y)% mod;
q[dl[k].x][dl[k].y-1]=1;
} else {
f[dl[k].x][dl[k].y-1]=(f[dl[k].x][dl[k].y-1]+f[dl[k].x][dl[k].y]*dl[k].x*dl[k].y)% mod;
}
}
if (dl[k].x>1) {
if (!q[dl[k].x-2][dl[k].y]) {
r++;dl[r].x=dl[k].x-2;dl[r].y=dl[k].y;
f[dl[k].x-2][dl[k].y]=(f[dl[k].x][dl[k].y]*c[dl[k].x][2])% mod;
q[dl[k].x-2][dl[k].y]=1;
} else {
f[dl[k].x-2][dl[k].y]=(f[dl[k].x-2][dl[k].y]+f[dl[k].x][dl[k].y]*c[dl[k].x][2])% mod;
}
}
if (dl[k].y>1) {
if (!q[dl[k].x+2][dl[k].y-2]) {
r++;dl[r].x=dl[k].x+2;dl[r].y=dl[k].y-2;
f[dl[k].x+2][dl[k].y-2]=(f[dl[k].x][dl[k].y]*c[dl[k].y][2])% mod;
q[dl[k].x+2][dl[k].y-2]=1;
} else {
f[dl[k].x+2][dl[k].y-2]=(f[dl[k].x+2][dl[k].y-2]+f[dl[k].x][dl[k].y]*c[dl[k].y][2])% mod;
}
}
}
int main()
{
int i,j,k,n,m,one,zero;
char ch;
scanf("%d%d%I64d", &n, &m, &mod);
memset(a,0,sizeof(a));
memset(q,0,sizeof(q));
memset(f,0,sizeof(f));
getchar();
for (i=1;i<=m;i++){
for (j=1;j<=n;j++) {
scanf("%c", &ch);
if (ch=='1') a[j]++;
}
getchar();
}
one=0;zero=0;
for (i=1;i<=n;i++)
if (a[i]==0) zero++;
else if (a[i]==1) one++;
for (i=0;i<=n;i++) {
c[i][0]=1;c[i][i]=1;
for (j=1;j<i;j++) c[i][j]=(c[i-1][j]+c[i-1][j-1])% mod;
}
l=1;r=1;
f[one][zero]=1;q[one][zero]=1;
dl[1].x=one;dl[1].y=zero;
while (l<=r) {
ditui(l);l++;
}
/*递推过程会出现问题,就是推出后面的,
原来的0位置放一个就会变成1,会改变前面的状态!
f[one][zero]=1;q[one][zero]=1;///2 1
for (i=n;i>=0;i--)
for (j=n;j>=0;j--) {
if (q[i+1][j+1]) {
f[i+1][j]=(f[i+1][j]+f[i+1][j+1]*(i+1)*(j+1))% mod;
q[i+1][j]=1;
}
if (q[i][j+2]) {
f[i+2][j]=(f[i+2][j]+f[i][j+2]*c[j+2][2])% mod;
q[i+2][j]=1;
}
if (q[i+2][j]) {
f[i][j]=(f[i][j]+f[i+2][j]*c[i+2][2])% mod;
q[i][j]=1;
}
}
*/
printf("%I64d\n", f[0][0]);
return 0;
}