01-K Code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 728 Accepted Submission(s): 209
Problem Description
Consider a code string S of N symbols, each symbol can only be 0 or 1. In any consecutive substring of S, the number of 0's differs from the number of 1's by at most K. How many such valid code strings S are there?
For example, when N is 4, and K is 3, there are two invalid codes: 0000 and 1111.
Input
The input consists of several test cases. For each case, there are two positive integers N and K in a line.
N is in the range of [1, 62].
K is in the range of [2, 5].
Output
Output the number of valid code strings in a line for each case.
Sample Input
Sample Output
2
14
//有一个长为 i 的 0-1 组成的序列,问任意连续长的子序列 0 1 数量差都不超过 m 的情况多少种
// dp[i][j][k] 意思是长为 i 的序列中 0 比 1 最多多 j 个,且长为 i 的序列中 1 比 0 最多多 k 个这样的序列的个数
所以 j,k最小为 0
dp [i][j][k] = dp[i-1][max(0,j-1)][k+1] // 填 1
+dp[i-1][j+1][max(0,k-1)] // 填 0
#include <bits/stdc++.h>
using namespace std;
#define LL long long int n,m;
LL dp[][][]; //长为 i 的序列,0比1最多多 j 个,且1比0最多多 k 个 int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,,sizeof(dp));
dp[][][]=;
for (int i=;i<=n;i++)
{
for (int j=;j<=m;j++)
{
for (int k=;k<=m;k++)
{
dp[i+][j+][max(k-,)]+=dp[i][j][k]; // 下一个数是 0
dp[i+][max(j-,)][k+]+=dp[i][j][k]; // 下一个数是 1
}
}
}
LL ans = ;
for (int i=;i<=m;i++)
for (int j=;j<=m;j++)
ans += dp[n][i][j];
printf("%lld\n",ans);
}
return ;
}
最新文章
- Hadoop学习笔记(1) 初识Hadoop
- Sqlite日期类型问题:该字符串未被识别为有效的 DateTime(String not recognized as a valid datetime)
- Redis学习-开始
- Windows&#174; 10 Mobile Technical Preview升级方法
- iOS - OC NSEnumerator		迭代器
- (DFS)zoj1008-Gnome Tetravex
- message 匹配不上grok正则 也会写入到elasticsearch
- Cocos2d-x中Vector使用
- jquery 获取选中的文字.当前光标所在的位置等jquery-fieldselection 插件
- ZCTF-ARM64-Re300
- OC——封装(初级与高级)
- 收敛 p75
- 【Demo 0004】Java基础-类封装性
- vue实现懒加载的几种方法
- MongoDB中数组类型相关的操作
- 团队第九次 # scrum meeting
- 地址栏输入url按回车发生了什么
- ceph mimc版本ceph-deploy安装与配置
- P1553 数字反转(升级版)(模拟)
- 20165221 JAVA第二周学习心得及体会
热门文章
- 微信团队原创分享:iOS版微信的内存监控系统技术实践
- B8:中介者模式 Mediator
- Web学习篇之---css基础知识(一)
- ERROR: ld.so: object &#39;/usr/lib64/libtcmalloc.so.4&#39; from LD_PRELOAD cannot be preloaded: ignored
- iOS蓝牙原生封装,助力智能硬件开发
- flash画图API:解析obj格式
- MATLAB 的字符串分析
- 监听器如何获取Spring配置文件
- 深入理解get和post的区别
- putty英文乱码---DM8168_ETV_V1.1(路视明)