3494. 国际象棋

题目链接

3494. 国际象棋

给一个 \(n\times m\) 大小的棋盘,放 \(k\) 个马,问有多少种方案

输入格式

输入一行包含三个正整数 \(N,M,K\),分别表示棋盘的行数、列数和马的个数。

输出格式

输出一个整数,表示摆放的方案数除以 \(1000000007\) (即 \(10^9+7\)) 的余数。

数据范围

对于 \(5\%\) 的评测用例,\(K=1\)
对于另外 \(10\%\) 的评测用例,\(K=2\)
对于另外 \(10\%\) 的评测用例,\(N=1\)
对于另外 \(20\%\) 的评测用例,\(N,M≤6,K≤5\)
对于另外 \(25\%\) 的评测用例,\(N≤3,M≤20,K≤12\)
对于所有评测用例,\(1≤N≤6,1≤M≤100,1≤K≤20\)

输入样例1:

1 2 1

输出样例1:

2

输入样例2:

4 4 3

输出样例2:

276

输入样例3:

3 20 12

输出样例3:

914051446

解题思路

状压dp

由于行数比较少,且每一列上的马只与前两列上的马有关,所以可以考虑状压dp:

  • 状态表示:\(f[i][p][t][q]\) 表示前 \(i\) 列,前两列包括第 \(i\) 列状态分别为 \(p,t\),共有 \(q\) 个马时的方案数

  • 状态计算:\(f[i][p][t][q]= \sum_{q=cnt}^k f[i-1][t][j][q-cnt]\),这里 \(j\) 为前前列的状态,\(cnt\)\(p\) 的二进制表示下 \(1\) 的个数

  • 时间复杂度:\(O(mk\times 2^{3n})\)

代码

// Problem: 国际象棋
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/3497/
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=6,mod=1e9+7;
int f[105][1<<6][1<<6][25],n,m,k;
int main()
{
    cin>>n>>m>>k;
    f[0][0][0][0]=1;
    for(int i=1;i<=m;i++)
    	for(int j=0;j<(1<<n);j++)
    		for(int t=0;t<(1<<n);t++)
    			if(j&(t>>2)||t&(j>>2))continue;
    			else
    			{
    				for(int p=0;p<(1<<n);p++)
    				{
    					if(j&(p>>1)||p&(j>>1)||t&(p>>2)||p&(t>>2))continue;
    					int cnt=__builtin_popcount(p);
	    				for(int q=cnt;q<=k;q++)
	    					f[i][p][t][q]=(f[i][p][t][q]+f[i-1][t][j][q-cnt])%mod;
    				}
    			}
    int res=0;
    for(int i=0;i<(1<<n);i++)
    	for(int j=0;j<(1<<n);j++)res=(res+f[m][i][j][k])%mod;
    cout<<res;
    return 0;
}
posted @ 2022-02-21 20:16  zyy2001  阅读(99)  评论(0编辑  收藏  举报