Codeforces Round #277.5 (Div. 2) F

F. Special Matrices
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

The first line of the input contains three integers nmmod (2 ≤ n ≤ 5000 ≤ m ≤ n2 ≤ 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.

Output

Print the remainder after dividing the required value by number mod.

Sample test(s)
input
3 1 1000
011
output
2
input
4 4 100500
0110
1010
0101
1001
output
1
Note

For the first test the required matrices are:

011
101
110

011
110
101

In the second test the required matrix is already fully given, so the answer is 1.

题意:给你n,m,mod,分别代表n*n的矩阵,m行已知的矩阵的分布,余数,让我们求的是在已知m行的情况下,满足每一行每一列的和为2并且每个点的数只能0,1的矩阵有多少个。

做法:当时不会做,参考了人家大神的方法。

F题O(n^2)解法。

设dp[i][j]表示已经有i列上有两个1,j列上有1个1,剩下的n-i-j列上还没有1。

那么,由于i的不递减,所以我们可以将这个作为递推的第一维。

dp[i][j + 2] += ( n - i - j ) * ( n - i - j - 1 ) / 2 * dp[i][j].(选择没有1的两列将其变成有一个1的两列)

dp[i + 1][j] += j * ( n - i - j ) * dp[i][j].(j>0)(选择一列有一个1的变成有两个1的,同时选择没有1的变成有一个1的)

dp[i + 2][j - 2] += j * ( j - 1 ) / 2 * dp[i][j].(j>1)(选择有一个1的两列变成有两个1的两列)

这样推法的可行性在于充分利用1的数量是固定的隐性条件,到最后终点dp[n][0]时的一定是合法的。

#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL long long
#define inf 0x0f0f0f0f
using namespace std;
LL dp[505][505];
int num[505][505],a[505];
int main()
{
    int n,m,mod;
    int i,j;
    char ss[1005];
    while(scanf("%d%d%d",&n,&m,&mod)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        memset(a,0,sizeof(a));
        memset(num,0,sizeof(num));
        getchar();
        for(i=1;i<=m;i++)
        {
            gets(ss);
            for(j=0;j<n;j++)
                num[i][j+1]=ss[j]-'0';
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(num[j][i]!=0)
                    a[i]++;
            }
        }
        int x,y;
        x=0;
        y=0;
        for(i=1;i<=n;i++)
        {
            if(a[i]==2)
                x++;
            if(a[i]==1)
                y++;
        }
        dp[x][y]=1;
        for(i=x;i<=n;i++)
            for(j=0;j<=n-i;j++)
            if(dp[i][j])
            {
                dp[i][j+2]+=(n-i-j)*(n-i-j-1)/2*dp[i][j]%mod;
                dp[i+1][j]+=j*(n-i-j)*dp[i][j]%mod;
                if(j>=2)
                dp[i+2][j-2]+=j*(j-1)/2*dp[i][j]%mod;
            }
        printf("%I64d\n",dp[n][0]);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值