[Codeforces] 1051D Bicolorings
\(\color{red}{\mathcal{Description}}\)
You are given a grid, consisting of \(2\) rows and \(n\) columns. Each cell of this grid should be colored either black or white.
Two cells are considered neighbours if they have a common border and share the same color. Two cells \(A\) and \(B\) belong to the same component if they are neighbours, or if there is a neighbour of \(A\) that belongs to the same component with \(B\) .
Let's call some bicoloring beautiful if it has exactly \(k\) components.
Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo \(998244353\) .
给定一个\(2\times n\) 的棋盘,可以对上面的格子黑白染色,求染色后棋盘上的联通块的个数正好为 \(k\) 的染色方案数
\(\color{red}{\mathcal{Input\ Format}}\)
The only line contains two integers \(n\) and \(k\) — the number of columns in a grid and the number of components required.
一行两个整数 \(n\) , \(k\)
\(\color{red}{\mathcal{Output\ Format}}\)
Print a single integer — the number of beautiful bicolorings modulo \(998244353\) .
一个整数,表示方案数\(\mod 998244353\) 后的结果
\(\color{red}{\mathcal{DataSize\ Agreement}}\)
$1≤n≤1000 $, \(1 \le k \le 2n\)
\(\color{red}{\mathcal{Solution}}\)
题目简洁明了(大意),考虑用动态规划
我们不妨先从此题的弱化版想起,也就是只有一行的时候
令 \(dp[i][j][k]\) 表示到第 \(i\) 列,产生了 \(j\) 个连通块,状态为 \(k\) 的时候的方案数,显然 \(k\) 只有两种状态: \(0\) 或 \(1\),我们可以得到转移方程(最好画图理解):
理解了上面这个转移方程,对于此题只是变成了 \(4\) 个状态,转移方程也自然出来了(最好画图理解)
初始化 \(dp[1][1][0]=dp[1][2][1]=dp[1][2][2]=dp[1][1][3] = 1\)
\(\color{red}{\mathcal{Code}}\)
#include <bits/stdc++.h>
#define LL long long
#define reg register
using namespace std;
const int kM = 1010, kC = 2010, kS = (1 << 1) + 2, mod = 998244353;
LL dp[kM][kC][kS] = {0}; //kM-列 kC-个数 kS-00 01 10 11
int N, K;
int main() {
scanf("%d%d", &N, &K);
dp[1][1][0] = dp[1][2][1] = dp[1][2][2] = dp[1][1][3] = 1;
for (reg int i = 2; i <= N; ++i) {
for (reg int j = 1; j <= i * 2; ++j) {
dp[i][j][0] = (dp[i-1][j][0] % mod + dp[i-1][j][1] % mod + dp[i-1][j][2] % mod + dp[i-1][j-1][3]) % mod;
dp[i][j][1] = (dp[i-1][j-1][0] % mod + dp[i-1][j][1] % mod + dp[i-1][j-2][2] % mod + dp[i-1][j-1][3] % mod) % mod;
dp[i][j][2] = (dp[i-1][j-1][0] % mod+ dp[i-1][j-2][1] % mod + dp[i-1][j][2] % mod + dp[i-1][j-1][3] % mod) % mod;
dp[i][j][3] = (dp[i-1][j-1][0] % mod + dp[i-1][j][1] % mod + dp[i-1][j][2] % mod + dp[i-1][j][3] % mod) % mod;
}
}
printf("%lld\n", (dp[N][K][0] % mod + dp[N][K][1] % mod + dp[N][K][2] % mod + dp[N][K][3] % mod) % mod);
return 0;
}
\(\color{red}{\mathcal{Source}}\)
\(Educational\ Codeforces\ Round 51\ [Rated\ for\ Div. 2]\)