D. Bicolorings
题目链接:http://codeforces.com/contest/1051/problem/D
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a grid, consisting of 22 rows and nn 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 AA and BB belong to the same component if they are neighbours, or if there is a neighbour of AA that belongs to the same component with BB.
Let's call some bicoloring beautiful if it has exactly kk components.
Count the number of beautiful bicolorings. The number can be big enough, so print the answer modulo 998244353998244353.
Input
The only line contains two integers nn and kk (1≤n≤10001≤n≤1000, 1≤k≤2n1≤k≤2n) — the number of columns in a grid and the number of components required.
Output
Print a single integer — the number of beautiful bicolorings modulo 998244353998244353.
Examples
input
Copy
3 4
output
Copy
12
input
Copy
4 1
output
Copy
2
input
Copy
1 2
output
Copy
2
Note
One of possible bicolorings in sample 11:
题目大意:给出两行n列的矩阵,只有两种颜色,问有多少种方案使最后联通块的个数为k
对于每一列只有四种情况11 10 01 00 记为1 2 3 4
设dp[i][j][k]为第i列 以1,2,3,4,结尾有j种方案
则 dp[i][j][1]可由dp[i-1][j][1] dp[i-1][j][2] dp[i][j][3] dp[i][j-1][4];
其余等情况和之前类似。
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define sca(x) scanf("%d",&x)
#define per(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x3f3f3f3f
#define LL long long
#define N 200005
#define inf 0x3f3f3f3f
const LL mod = 998244353;
LL dp[1005][2005][4];
int main()
{
int n,k;
scanf("%d%d",&n,&k);
dp[1][1][1]=1,dp[1][2][2]=1,dp[1][2][3]=1,dp[1][1][4]=1;
rep(i,2,n)
{
rep(j,1,k)
{
dp[i][j][1]=(dp[i][j][1]+dp[i-1][j][1])%mod;
dp[i][j][1]=(dp[i][j][1]+dp[i-1][j][2])%mod;
dp[i][j][1]=(dp[i][j][1]+dp[i-1][j][3])%mod;
dp[i][j][1]=(dp[i][j][1]+dp[i-1][j-1][4])%mod;
dp[i][j][2]=(dp[i][j][2]+dp[i-1][j-1][1])%mod;
dp[i][j][2]=(dp[i][j][2]+dp[i-1][j][2])%mod;
if(j>=2)
dp[i][j][2]=(dp[i][j][2]+dp[i-1][j-2][3])%mod;
dp[i][j][2]=(dp[i][j][2]+dp[i-1][j-1][4])%mod;
dp[i][j][3]=(dp[i][j][3]+dp[i-1][j-1][1])%mod;
if(j>=2)
dp[i][j][3]=(dp[i][j][3]+dp[i-1][j-2][2])%mod;
dp[i][j][3]=(dp[i][j][3]+dp[i-1][j][3])%mod;
dp[i][j][3]=(dp[i][j][3]+dp[i-1][j-1][4])%mod;
dp[i][j][4]=(dp[i][j][4]+dp[i-1][j-1][1])%mod;
dp[i][j][4]=(dp[i][j][4]+dp[i-1][j][2])%mod;
dp[i][j][4]=(dp[i][j][4]+dp[i-1][j][3])%mod;
dp[i][j][4]=(dp[i][j][4]+dp[i-1][j][4])%mod;
//cout<<dp[i][j][1]<<" "<<dp[i][j][2]<<" "<<dp[i][j][3]<<" "<<dp[i][j][4]<<endl;
}
}
LL sum=0;
rep(i,1,4)sum=(sum+dp[n][k][i])%mod;
cout<<sum<<endl;
}