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. Java实现对象的序列化

    什么是对象的序列化? 序列化:把对象转化成字节序列的过程就是对象的序列化:反序列化:把字节序列转化成对象的过程就是对象的反序列化.单看概念比较抽象,但是看代码就会明白. 对象序列化的用途 1.Java ...

  2. statefulSet 原理理解

    1.  svc(vip)                       --              deployment 2.headless(none)                --     ...

  3. 15-(基础入门篇)GPRS(Air202)GPIO控制点亮一个灯

    https://www.cnblogs.com/yangfengwu/p/9967027.html 现在点亮它,说一下哈,都过去好久了,不知道大家都在用哪个版本的库 http://www.openlu ...

  4. 7-51单片机ESP8266学习-AT指令(8266TCP服务器,编写自己的C#TCP客户端发信息给单片机控制小灯的亮灭)

    http://www.cnblogs.com/yangfengwu/p/8780182.html 自己都是现做现写,如果想知道最终实现的功能,请看最后 先把源码和资料链接放到这里 链接: https: ...

  5. [Socket]Socket聊天小程序

    一个简单是Socket聊天小程序,读写操作在不同的线程中.服务器端采用线程池. 1.Server import java.io.IOException; import java.net.ServerS ...

  6. springboot @Value 获取计算机中绝对路径文件的内容

    默认情况下使用 @Value("aaa.txt") private Resource txtResource; 这样获取到的是项目classpath 下的 aaa.txt 如果想获 ...

  7. nginx的web缓存服务环境部署记录

    web缓存位于内容源Web服务器和客户端之间,当用户访问一个URL时,Web缓存服务器会去后端Web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的URL,Web缓存服务器直接输 ...

  8. JAVA中使用MD5加密实现密码加密

    1.新建Md5.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package c ...

  9. 【CV】ICCV2015_Unsupervised Learning of Spatiotemporally Coherent Metrics

    Unsupervised Learning of Spatiotemporally Coherent Metrics Note here: it's a learning note on the to ...

  10. HTML编码规则

    <!DOCTYPE html><!-- 为每一个HTML页面的第一行添加标准模式 --> <html lang="en-us"> <!-- ...