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 $$$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$$$.

Input

The only line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 1000$$$, $$$1 \le k \le 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 $$$998244353$$$.

Examples
Input
3 4
Output
12
Input
4 1
Output
2
Input
1 2
Output
2
Note

One of possible bicolorings in sample $$$1$$$:

题意
给一个2行n列的矩阵填上黑色和白色,求连通块个数为k个的填色方案数量(mod 998244353)
分析
因为只有两行,为n-1列的矩阵增加1列的情况数只有很少,容易想到用 $$$(i, k)$$$ 表示 $$$i$$$ 列有 $$$k$$$ 个连通块的矩阵, 但是它在向 $$$i+1$$$ 列的矩阵转移时,需要知道最后一列的状态,所以可以用 $$$0$$$, $$$1$$$, $$$2$$$, $$$3$$$表示最后一列为 $$$00$$$, $$$01$$$, $$$10$$$, $$$11$$$,那么状态就增加一维变成 $$$(i, k, s)$$$,然后就是分析递推关系:

$$$(i,k,0)$$$ 的矩阵,可以由 $$$i-1$$$ 列的矩阵添加一列 $$$00$$$ 得到,当它的结尾为 $$$00$$$, $$$01$$$, $$$10$$$, $$$11$$$时,分别会让连通块个数:不变,不变,不变,+1,所以 $$$(i,k,0)$$$由 $$$(i-1,k,0)$$$, $$$(i-1,k,1)$$$, $$$(i-1,k,2)$$$, $$$(i-1,k-1,3)$$$得到:
$$$$$$
\begin{align}
dp[i][k][0,0]=~~~& dp[i-1][k][0,0]\\
+& dp[i-1][k][0,1]\\
+& dp[i-1][k][1,0]\\
+& dp[i-1][k-1][1,1]
\end{align}
$$$$$$
$$$(i,k,1)$$$的矩阵同理,为$$$i-1$$$列的矩阵添加 $$$01$$$,当结尾为 $$$00$$$, $$$01$$$, $$$10$$$, $$$11$$$时,分别会使连通块的个数:+1,不变,+2,+1,所以$$$(i,k,1)$$$由$$$(i-1,k-1,0)$$$,$$$(i-1,k,1)$$$,$$$(i-1,k-2,2)$$$,$$$(i-1,k-1,3)$$$得到:
$$$$$$
\begin{align}
dp[i][k][0,1]=~~~& dp[i-1][k-1][0,0]\\
+& dp[i-1][k][0,1]\\
+& dp[i-1][k-2][1,0]\\
+& dp[i-1][k-1][1,1]
\end{align}
$$$$$$
(i,k,2)同理可得:
$$$$$$
\begin{align}
dp[i][k][1,0]=~~~& dp[i-1][k-1][0,0]\\
+& dp[i-1][k-2][0,1]\\
+& dp[i-1][k][1,0]\\
+& dp[i-1][k-1][1,1]
\end{align}
$$$$$$
(i,k,3)同理可得:
$$$$$$
\begin{align}
dp[i][k][1,1]=~~~& dp[i-1][k-1][0,0]\\
+& dp[i-1][k][0,1]\\
+& dp[i-1][k][1,0]\\
+& dp[i-1][k][1,1]
\end{align}
$$$$$$
于是得到了完整的递推公式,只需要从下面的状态开始,
$$$$$$
\begin{align}
dp[1][1][0,0]=1\\
dp[1][2][0,1]=1\\
dp[1][2][1,0]=1\\
dp[1][1][1,1]=1
\end{align}
$$$$$$
就能推到出所有的状态,最后对dp[n][k]的所有情况求和就是答案了。

注意当k为1时,是不存在k-2的状态的,需要特判一下避免超出数组范围

总结
动态规划的状态定义很关键,必须抓住状态之间的联系;递推式的推导也需要深入思考
代码
#include<stdio.h>
typedef long long LL;
#define mod 998244353
int dp[][][] = {};
int main() {
int n, lm;
scanf("%d %d", &n, &lm);
//初始化
dp[][][] = ;//
dp[][][] = ;//
dp[][][] = ;//
dp[][][] = ;//
LL temp=;
for (int i = ; i <= n; ++i) {
for (int k = ; k <= (i << ); ++k) {
temp = ;//使用temp求和来避免溢出
temp =temp
+ dp[i - ][k][]//
+ dp[i - ][k][]//
+ dp[i - ][k][]//
+ dp[i - ][k - ][];//
dp[i][k][] = temp % mod;
temp = ;
temp = temp
+ dp[i - ][k][]//
+ dp[i - ][k-][]//
+ (k>=?dp[i - ][k - ][]:)//
+ dp[i - ][k-][];//
dp[i][k][] = temp%mod;
temp = ;
temp = temp
+ (k>=?dp[i - ][k - ][]:)//
+ dp[i - ][k-][]//
+ dp[i - ][k][]//
+ dp[i - ][k-][];//
dp[i][k][] = temp%mod;
temp = ;
temp = temp
+ dp[i - ][k][]//
+ dp[i - ][k - ][]//
+ dp[i - ][k][]//
+ dp[i - ][k][];//
dp[i][k][] = temp%mod;
temp = ;
}
}
LL ans = ;
ans = ans + dp[n][lm][] + dp[n][lm][] + dp[n][lm][] + dp[n][lm][];
ans = ans%mod;
printf("%I64d\n", ans);
}

codeforces 1051 D. Bicolorings (DP)的更多相关文章

  1. Codeforces 1051 D.Bicolorings(DP)

    Codeforces 1051 D.Bicolorings 题意:一个2×n的方格纸,用黑白给格子涂色,要求分出k个连通块,求方案数. 思路:用0,1表示黑白,则第i列可以涂00,01,10,11,( ...

  2. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  3. CodeForces - 1051D Bicolorings(DP)

    题目链接:http://codeforces.com/problemset/problem/1051/D 看了大佬的题解后觉着是简单的dp,咋自己做就做不来呢. 大佬的题解:https://www.c ...

  4. codeforces Hill Number 数位dp

    http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits: ...

  5. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  6. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

  7. codeforces round367 div2.C (DP)

    题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...

  8. CodeForces 176B Word Cut dp

    Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...

  9. codeforces 148D之概率DP

    http://codeforces.com/problemset/problem/148/D D. Bag of mice time limit per test 2 seconds memory l ...

随机推荐

  1. 查看电脑本机的ip地址

    第一步:打开cmd 第二步:输入:ipconfig,回车 第三步:找到无线局域网适配器下的IPv4地址,那就是本机的ip地址

  2. RBAC 基于权限的访问控制 serviceaccount -- clusterRole clusterRoleBinding

    1.Role , RoleBinding 的作用对象都是namespace. 2.通过RoleRef,可以看到,RoleBinding对象通过名字,直接引用前面定义的Role,实现subject(us ...

  3. 关于TerraBuilder的扩展开发

    熟悉Skyline的朋友,可能会发现,在最新的6.6的产品体系中,TerraBuilder中用于生成三维地形场景的模块,改成了TerrainBuilder. 通常情况下,这款软件模块,我们主要用它来进 ...

  4. 解决Skyline 6.5版本中3DML模型单体化后外部网页挂接问题

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 7、存储类 & 作用域 & 生命周期 & 链接属性

    概念解析 存储类 存储类就是存储类型,也就是描述C语言变量在何种地方存储. 内存有多种管理方法:栈.堆.数据段.bss段..text段······一个变量的存储类属性就是描述这个变量存储在何种内存段中 ...

  6. Literal 字面值 字面量 的理解

    Literal 字面值 字面量 Literal, 在程序语言中,指表示某种数据值的符码.如,123 是整数值符码, 3.14 是浮点值符码,abcd 是字串值符码,True, False, 是逻辑值符 ...

  7. NOIP2002-2017提高组题解

    给个人认为比较难的题目打上'*' NOIP2002(clear) //一个很吼的贪心题,将平均数减掉之后从左往右将影响消除 #include<bits/stdc++.h> using na ...

  8. RabbmitMQ-Publish/Subscribe

    之前的学习中,我们了解的工作队列实现的是:一个消息只发送到一个消费者. 现在我们来学习一下新模式:发布/订阅模式 之前我们在原理中介绍了exchange,但好像并没有使用.而是直接往队列里发消息和取消 ...

  9. 使用IE浏览提示:该页面无法显示

    问题描述: 我们有一个外部招聘的网站,DBA反馈新版上线过后首页集成的登录部分页面无法打开,一直显示“该页面无法显示”! 问题排查: 1.因为我本身也不是负责这一块的业务,刚开始以为是网站本身程序的问 ...

  10. ORA-00020:maximum number of processes (150) exceeded

    异常的含义 超过最大的进程数 我们使用下面的语句可以查看与进程(process)的相关参数: 如上所示,这里的最大进程数是150. 问题可能存在的原因 1.应用程序在使用数据库连接池时,使用完成后没有 ...