计数用红色着色m灰色块的方法,其中红色块应以计数至少为n的组进行
Count ways of coloring m grey blocks with red color, in which red blocks should be in groups with count at least n
我想计算用白色为i黑色块着色的方法,其中红色块应该在计数至少为j的组中。
C(m)
是m
块着色方法的数量(固定n
(。
如果m<n
,则C(m)=1
,因为不能出现红色方块。
否则,颜色要么以灰色块结尾,要么全部为红色,要么以灰色块结尾,后跟n
个或多个红色块。
也就是说,如果m>=n
,C(m)=C(m-1)+1+sum(C(m-i-1) for i=n..m-1)
.重写这给了C(m)=1+C(m-1)+sum(C(i) for i=0..(m-n-1))
.
只要小心一点,我们可以在O(m(时间和空间中计算它。需要注意的是,总和可以增量计算。
这里有一种方法可以做到这一点(python,但应该很容易转换为你想要的任何语言(:
def count(m, n):
C = [0] * (m+1)
s = 0
for i in range(m+1):
if i-n-1 >= 0:
s += C[i-n-1]
C[i] = 1 if i<n else C[i-1] + 1 + s
return C[m]
print(count(7, 3))
稍微小心一点,我们可以注意到我们只在任何时间点访问C
的最后n+1
元素,因此我们可以将空间使用量减少到 O(n(:
def count(m, n):
C = [0] * (n+1)
s = 0
for i in range(m+1):
if i-n-1 >= 0:
s += C[(i-n-1)%(n+1)]
C[i%(n+1)] = 1 if i<n else C[(i+n)%(n+1)] + 1 + s
return C[m%(n+1)]
print(count(7, 3))
这是一个可能的程序:
#include <iostream>
#include <vector>
int count_rec(int m, int n, std::vector<int>& cache)
{
// Count starts with 1 for no blocks filled
int c = 1;
// i = starting position of current block
for (int i = 0; i <= m - n; i++)
{
// j = number of filled blocks
for (int j = n; j <= m - i; j++)
{
// r = number of blocks available after this block
int r = m - j - i;
// Check if number of combinations for r has already been calculated
if (cache[r] < 0)
{
// If not calculate (subtract 1 block gap)
cache[r] = count_rec(r - 1, n, cache);
}
// Get cached value
c += cache[r];
}
}
return c;
}
int count(int m, int n)
{
if (m <= 0) return 0;
if (n <= 0)
{
n = 1;
}
std::vector<int> cache(m, -1);
return count_rec(m, n, cache);
}
int main()
{
std::cout << "count(7, 3) = " << count(7, 3) << std::endl;
return 0;
}
输出:
count(7, 3) = 17
相关文章:
- VSCode-有一个红色下划线,但程序构建和运行正确,并且出现配音错误
- 无法输出对象的红色/绿色/蓝色值
- OpenCV C++将灰色图像组合成彩色图像
- 计数用红色着色m灰色块的方法,其中红色块应以计数至少为n的组进行
- Qmage:如何将灰色图像转换为RGB热图
- 如何在QT中为QInputDialog输入密码时设置背景非活动和灰色?
- 如何阻止LWA_COLORKEY制作灰色轮廓?
- 如何通过组合红色和蓝色无符号字节来优化混合?
- glTexImage2D 不起作用,但将 FragColor 设置为 vec4(1.0, 0, 0, 1.0) 会给我红色图像
- 为什么延迟太高,即使只是 RGB 到灰色转换 (Vivado HLS)?
- 如何在灰色区域增长明亮像素?OpenCV C++
- 如何将控制台输出的文本颜色更改为红色,但如果从Powershell或CMD运行,背景将保持相同的颜色
- 使用 C++ 库 GLEW 在 openGl 中绘制一个红色三角形
- OpenCV4 Imshow函数打开一个空的灰色框
- 红色伪像通过GSTREAMER和QT5可视化RTSP流
- 用高斯Seidel红色黑色求解1D泊松方程
- D3D11:渲染(深度)到纹理会导致红色方块,正常渲染有效
- 选择一些用灰色代码编码的数字
- C 读取/写作PPM图像文件灰色图像
- 如何实现std :: set(红色/黑色树)前迭代