D. Bicolorings
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:
菜鸡被DP虐的日常,,,没怎么写过dp的题,一直在想四种状态怎么转移,原来要开三维、、、
开了三维后就好懂多了。
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <queue>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 1010
#define M 2100
const int INF = 0x3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-5;
const double PI = acos(-1);
#define fi first
#define se second
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)
int n, k;
ll dp[N][M][4];
int main()
{
#ifndef ONLINE_JUDGE
freopen("data.txt", "r", stdin);
#endif
dp[1][1][0] = 1;
dp[1][1][1] = 1;
dp[1][2][2] = 1;
dp[1][2][3] = 1;
rep(i, 2, 1000) rep(j, 1, i * 2) {
dp[i][j][0] = (dp[i - 1][j][0] + dp[i - 1][j][2] + dp[i - 1][j][3] + dp[i - 1][j - 1][1]) % mod;
dp[i][j][1] = (dp[i - 1][j][1] + dp[i - 1][j][2] + dp[i - 1][j][3] + dp[i - 1][j - 1][0]) % mod;
if(j >= 2) {
dp[i][j][2] = (dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1] + dp[i - 1][j][2] + dp[i - 1][j - 2][3]) % mod;
dp[i][j][3] = (dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1] + dp[i - 1][j - 2][2] + dp[i - 1][j][3]) % mod;
}
}
cin >> n >> k;
ll ans = 0;
rep(i, 0, 3) ans = (ans + dp[n][k][i]) % mod;
cout << ans << endl;
return 0;
}