Educational Codeforces Round 51 (Rated for Div. 2)(dp)

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.

扫描二维码关注公众号,回复: 3297984 查看本文章

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

3 4

output

12

input

4 1

output

2

input

1 2

output

2

Note

One of possible bicolorings in sample 11:

题意:给你一个2*n的矩阵,里面可以填黑白两种颜色,求连通块为k的矩阵种类

分析:由于只有两行,有i列,可以想到用dp[i][j]来表示有i列的矩阵有j个连通块,但要进行状态转移时第i列的颜色情况有4钟(黑,黑),(黑,白),(白,黑),(白,白),分别用(0,0),(0,1),(1,0),(1,1)来表示,故还要增加一维状态表示第i列的颜色情况,dp[i][j][k]中k表示第i列是以上4种情况的哪一种

步骤:一、确定初始状态,dp[1][1][0]=1,表示只有1列全为黑,只有1种,下同

dp[1][2][1]=1,dp[1][2][2]=1,(一黑一白),dp[1][1][3]=1(全白)

二、状态转移方程:

dp[i][j][0]为例:第i列为全黑,那么它可以由i-1列再加上一列转移过来,第i-1列分别填全黑,黑白,白黑,白白分别会增加0,0,0,1个连通块,故dp[i][j][0]=dp[i-1][j][0]+dp[i-1][j][1]+dp[i-1][j][2]+dp[i-1][j-1][3]; 其余同理

ac code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
ll dp[1001][2001][4]={0};
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    dp[1][1][0]=1;
    dp[1][2][1]=1;
    dp[1][2][2]=1;
    dp[1][1][3]=1;
    for(int i=2;i<=n;i++)
      for(int j=1;j<=(i<<1);j++)///最多有2*i个连通块
        {
            dp[i][j][0]=(dp[i-1][j][0]+dp[i-1][j][1]+dp[i-1][j][2]+dp[i-1][j-1][3])%mod;
            dp[i][j][1]=(dp[i-1][j-1][0]+dp[i-1][j][1]+dp[i-1][j-2][2]+dp[i-1][j-1][3])%mod;
            dp[i][j][2]=(dp[i-1][j-1][0]+dp[i-1][j-2][1]+dp[i-1][j][2]+dp[i-1][j-1][3])%mod;
            dp[i][j][3]=(dp[i-1][j-1][0]+dp[i-1][j][1]+dp[i-1][j][2]+dp[i-1][j][3])%mod;
        }
    ll ans=0;
    for(int i=0;i<=3;i++)
        ans=(ans+dp[n][k][i])%mod;
    printf("%lld\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shaohang_/article/details/82806731