Educational Codeforces Round 51 (Rated for Div. 2) D. Bicolorings

本文介绍了一种使用三维动态规划方法解决特定网格上美丽二染色方案计数的问题。网格由2行n列组成,目标是找到恰好包含k个连通组件的染色方案数量。通过维护前缀和状态,文章详细展示了如何在每个位置更新四种可能的状态,最终求得答案模998244353。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值