E. Inverse Coloring
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a square board, consisting of $$$n$$$ rows and $$$n$$$ columns. Each tile in it should be colored either white or black.

Let's call some coloring beautiful if each pair of adjacent rows are either the same or different in every position. The same condition should be held for the columns as well.

Let's call some coloring suitable if it is beautiful and there is no rectangle of the single color, consisting of at least $$$k$$$ tiles.

Your task is to count the number of suitable colorings of the board of the given size.

Since the answer can be very large, print it modulo $$$998244353$$$.

Input

A single line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 500$$$, $$$1 \le k \le n^2$$$) — the number of rows and columns of the board and the maximum number of tiles inside the rectangle of the single color, respectively.

Output

Print a single integer — the number of suitable colorings of the board of the given size modulo $$$998244353$$$.

Examples
Input
1 1
Output
0
Input
2 3
Output
6
Input
49 1808
Output
359087121
Note

Board of size $$$1 \times 1$$$ is either a single black tile or a single white tile. Both of them include a rectangle of a single color, consisting of $$$1$$$ tile.

Here are the beautiful colorings of a board of size $$$2 \times 2$$$ that don't include rectangles of a single color, consisting of at least $$$3$$$ tiles:

The rest of beautiful colorings of a board of size $$$2 \times 2$$$ are the following:

题意
矩阵的方格可以是黑色或白色,求满足条件的矩阵个数:相邻两列/行的颜色要么完全相同,要么完全相反,并且连续的黑色/白色的区域面积必须小于$$$K$$$
分析
假设现在已经有一个满足条件的矩阵,那么以第一行为基准,所有行要么和它相同,要么相反,如果用0/1分别表示相反/相同的话,从第一行到第n行就可以表示为一个长度为n的0/1序列,同理第一列到第n列也可以表示为长度为n的0/1序列。
当同时知道了行的0/1序列和列的0/1序列,只要再知道矩阵任何一个方格的颜色,就能唯一的表示一个矩阵了。

观察发现,同一种颜色的区域都是矩形,而且它们的大小来自于行和列上连续的0或1。如果把行和列的序列连续的0/1的个数记录下来,比如上面的例子中,行是2,3,3,列是1,2,2,1,2,我们就得到了两个新的序列,并且两个序列的数相乘就是所有色块的面积。
         
自然想到,行中最大的数乘以列中最大的数小于k就是充分必要条件,于是转而分析序列的最大值。
不难发现,序列的最大值可以从1取到n,如果已经知道最大值分别为1,2,3,..n的序列的个数,那么我们只需要让两个序列的最大值两两匹配,遍历所有乘积小于k的情况,就能求出总方案个数。
接下来的问题就是怎么取求最大值分别为1,2,3,..n的序列的个数,也就是说需要解决怎么求最大值为m,和为n的序列的个数。
假设用$$$(n,m)$$$代表最大值为$$$m$$$和为$$$n$$$的任意序列$$$a_1,a_2,...,a_t$$$,注意序列每个数字其实是有颜色的,那么总可以在$$$a_1$$$前面,添加一个颜色相反的数字$$$a_0$$$,如果它不大于$$$m$$$,那么就得到了一个$$$(n+a_0,m)$$$,按照这个思路,如果用$$$dp[n][m]$$$表示序列的个数,可以构造出状态转移方程:
$$$dp[n][m]=dp[n-1][m]+dp[n-2][m]+...+dp[n-m][m]$$$
含义就是$$$(n,m)$$$可以通过在$$$(n-1,m)$$$前面添加$$$1$$$,$$$(n-2,m)$$$前面添加$$$2$$$,...,的方法得到。
注意到一件事,$$$dp[n][m]$$$,当$$$n$$$小于等于$$$m$$$的时候,含义就是和为$$$n$$$的序列,且没有任何限制,那么个数就是$$$2^n$$$
所以状态转移方程就是:
$$$$$$dp[n][m]=
\begin{cases}
\sum_{i=1}^{m} dp[n-i][m], & {n\gt m} \\[2ex]
2^n, & {n\le m}
\end{cases}
$$$$$$
进一步发现,对于$$$n>m$$$的部分
$$$$$$\begin{align}
dp[n][m] &=dp[n-1][m]+dp[n-2][m]+...+dp[n-m][m]\\
&=dp[n-1][m]+(dp[n-1][m]-dp[n-m-1][m])\\
&=2*dp[n-1][m]-dp[n-m-1][m]
\end{align}$$$$$$
所以最终的状态转移方程就是:
$$$$$$dp[n][m]=
\begin{cases}
2*dp[n-1][m]-dp[n-m-1][m], & {n\gt m} \\[2ex]
2^n, & {n\le m}
\end{cases}
$$$$$$

总结
最开始的时候推了错的转移方程,$$$dp[n][m]=dp[n-m][m]+dp[n][m-1]$$$,发现得到的仅仅是组合的个数,并不是序列的个数,分析一下发现,这个转移方程相当于限定了序列必须是递减的,和正确的转移方程差了很多中间项
代码
#include<stdio.h>
typedef long long LL;
#define mod 998244353
int dp[][];
#define min(a,b) ((a)<(b)?(a):(b))
inline void add(int &a, int b) { a += b; if (a>mod)a -= mod; if (a<)a += mod; } void init() {
int temp;
dp[][] = ; dp[][] = ;
for (int t = ; t <= ; ++t) {
dp[t][t] = (dp[t - ][t - ] << ) % mod;
dp[][t] = ;
}
for (int k = ; k <= ; ++k) {
for(int n=;n<=k;++n){
dp[n][k] = dp[n][n];
}
for (int n = k + ; n <= ; ++n) {
dp[n][k] = (*dp[n-][k]%mod-dp[n-k-][k]+mod)%mod;
}
}
} int main() {
int n, k;
init();
scanf("%d %d", &n, &k);
int ans = ;
LL help;
for (int i = ; i <= n; ++i) {
help = dp[n][i] - dp[n][i - ];
help = help*(dp[n][min((k - ) / i,n)]) % mod;
add(ans, help);
}
ans = (ans<<) % mod;
printf("%d\n", ans);
}

codeforces 1027 E. Inverse coloring (DP)的更多相关文章

  1. Codeforces 1027E Inverse Coloring 【DP】

    Codeforces 1027E Inverse Coloring 题目链接 #include<bits/stdc++.h> using namespace std; #define N ...

  2. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  3. 【CF1027E】Inverse Coloring(DP)

    题意:给出一个n*n的矩阵,要求在每个位置涂上黑/白色, 要求满足:任意相邻的两行,其颜色要么完全相同,要么完全相反 任意相邻的两列,其颜色也要么相同要么完全相反 且这个矩形中,不存在任意一个大小大于 ...

  4. codeforces 1027E. Inverse Coloring(计数)

    一开始发现的性质是确定了第一行后,后面的行只需要考虑和前面的行相同或者不同,整个过程只需要考虑行,构出的图一定符合性质(即同样满足列的性质),但是接下来死活定义不出状态,事实证明自己还是想的太少了 思 ...

  5. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

  6. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces 543D. Road Improvement (树dp + 乘法逆元)

    题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...

  8. Codeforces 467C. George and Job (dp)

    题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...

  9. Codeforces 706 C. Hard problem (dp)

    题目链接:http://codeforces.com/problemset/problem/706/C 给你n个字符串,可以反转任意一个字符串,反转每个字符串都有其对应的花费ci. 经过操作后是否能满 ...

随机推荐

  1. #2017-2018-1 20155327 《信息安全系统设计基础》实现mypwd

    2017-2018-1 20155327 <信息安全系统设计基础>实现mypwd Linux pwd命令用于显示工作目录. 执行pwd指令可立刻得知您目前所在的工作目录的绝对路径名称. p ...

  2. 20155333 2016-2017-2《Java程序设计》课程总结

    20155333 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:你期望的师生关系是什么? 预备作业2:体会做中学(Learing By Doi ...

  3. sql语句-2-字符串数字日期时间

  4. plsql高级查询命令

    一.DDL数据定义语言:表操作 1.新建表 SQL> create table good(id number,name varchar2(10)); 添加注释 SQL> comment o ...

  5. 【转载】关于RenderTarget的注意事项

    原文:关于RenderTarget的注意事项 1. 设置一个RenderTarget会导致viewport变成跟RenderTarget一样大 2. 反锯齿类型必须跟DepthStencilBuffe ...

  6. ptyhon基础篇 day1

    1.变量 print('helloworld!') name = 'alex' name2 = 'jack' print(name,name2) 2.input #用户输入 username = in ...

  7. 【Loj10222】佳佳的Fibonacci

    题面 题解 可以发现\(T(n)\)无法用递推式表示. 于是我们做如下变形: \[ T(n) = \sum _ {i = 1} ^ n i \times f_i \\ S(n) = \sum _ {i ...

  8. 洛谷3197&bzoj1008 越狱

    洛谷3197&bzoj1008 越狱 Luogu bzoj 题解 所有状态减合法状态.SBT 答案为\(m^n-m*(m-1)^{n-1}\)太SB不解释 注意取膜的问题.相减可能减出负数,而 ...

  9. 世界杯足彩怎么买划算?机器学习AI告诉你答案(含预测)

    本文首发于InfoQ公众号头条. 四年一度的世界杯又来了,作为没什么时间看球的码农,跟大家一样,靠买买足彩给自己点看球动力和乐趣, 然而总是买错球队,面对各种赔率也不知道怎么买才划算,足彩是不是碰大运 ...

  10. Linux中新增硬盘的分区,格式化与挂载

    Linux中新增硬盘的分区,格式化与挂载 本篇教程内容为怎样对Linux新增硬盘进行挂载,所以如果有准备新增硬盘但是有各种问题的,请参看本篇教程. 我们先说说什么是挂载? 我们知道Linux中的所有设 ...