简单DP---Adjacent Bit Counts

本文介绍了一种使用动态规划解决的问题,旨在计算长度为n的二进制字符串中,相邻1位的数量等于给定值k的不同组合数。通过递推公式,文章详细解释了如何高效地解决这个问题,并提供了完整的AC代码实现。

Description

  For a string of n bits x1, x2, x3,…, xn, the adjacent bit count of the string (AdjBC(x)) is given by

  x_{1}*x_{2}+x_{2}*x_{3}+x_{3}*x_{4}...x_{n-1}*x_{n}

which counts the number of times a 1 bit is adjacent to another 1 bit. For example:

AdjBC(011101101) = 3

AdjBC(111101101) = 4

AdjBC(010101010) = 0

  Write a program which takes as input integers n and k and returns the number of bit strings x of n bits (out of 2^{n} ) that satisfy AdjBC(x) = k. For example, for 5 bit strings, there are 6 ways of getting AdjBC(x) = 2:

11100, 01110, 00111, 10111, 11101, 11011

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set is a single line that contains the data set number, followed by a space, followed by a decimal integer giving the number (n) of bits in the bit strings, followed by a single space, followed by a decimal integer (k) giving the desired adjacent bit count. The number of bits (n) will not be greater than 100 and the parameters n and k will be chosen so that the result will fit in a signed 32-bit integer.

Output

For each data set there is one line of output. It contains the data set number followed by a single space, followed by the number of n-bit strings with adjacent bit count equal to k.

Sample Input

10

1 5 2

2 20 8

3 30 17

4 40 24

5 50 37

6 60 52

7 70 59

8 80 73

9 90 84

10 100 90

Sample Output

1 6

2 63426

3 1861225

4 168212501

5 44874764

6 160916

7 22937308

8 99167

9 15476

10 23076518

题意

简单来说就是长度为n时按照所给公式得到的值为k的串的组成方式有几种

思路

每周组队赛抓的一道题,当时没做出来,后来才发现是一个简单的DP,不难想到长度为n的串可以由长度为n-1的串得到,只需要分别考虑末尾是以1还是0结尾即可,若长度为n时末尾为0,则当n-1的串添加0时值是不变的,可得到它为n-1长度时值已经为k,同理可以推出末尾为1的情况,即状态转移方程为:

dp[i][j][0] = dp[i-1][j][0] + dp[i-1][j][1];

dp[i][j][1] = dp[i-1][j][0] + dp[i-1][j-1][1];

AC代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>

using namespace std;

const int MAXN = 1e3+10;

int dp[MAXN][MAXN][2];//三维分别为长度,值,0结尾还是1结尾

int main()
{
    int T;
    scanf("%d",&T);
    int x,n,k;
    while(T--){
        scanf("%d%d%d",&x,&n,&k);
        memset(dp,0,sizeof(dp));
        dp[1][0][1] = 1;
        dp[1][0][0] = 1;
        for(int i=2;i<=n;i++){
            for(int j=0;j<=k;j++){
                dp[i][j][0] = dp[i-1][j][0] + dp[i-1][j][1];
                dp[i][j][1] = dp[i-1][j][0] + dp[i-1][j-1][1];
            }
        }
        printf("%d ",x);
        printf("%d\n",dp[n][k][0]+dp[n][k][1]);
    }
    return 0;
}

 

根据原作 https://pan.quark.cn/s/0ed355622f0f 的源码改编 野火IM解决方案 野火IM是专业级即时通讯和实时音视频整体解决方案,由北京野火无限网络科技有限公司维护和支持。 主要特性有:私有部署安全可靠,性能强大,功能齐全,全平台支持,开源率高,部署运维简单,二次开发友好,方便与第三方系统对接或者嵌入现有系统中。 详细情况请参考在线文档。 主要包括一下项目: 野火IM Vue Electron Demo,演示如何将野火IM的能力集成到Vue Electron项目。 前置说明 本项目所使用的是需要付费的,价格请参考费用详情 支持试用,具体请看试用说明 本项目默认只能连接到官方服务,购买或申请试用之后,替换,即可连到自行部署的服务 分支说明 :基于开发,是未来的开发重心 :基于开发,进入维护模式,不再开发新功能,鉴于已经终止支持且不再维护,建议客户升级到版本 环境依赖 mac系统 最新版本的Xcode nodejs v18.19.0 npm v10.2.3 python 2.7.x git npm install -g node-gyp@8.3.0 windows系统 nodejs v18.19.0 python 2.7.x git npm 6.14.15 npm install --global --vs2019 --production windows-build-tools 本步安装windows开发环境的安装内容较多,如果网络情况不好可能需要等较长时间,选择早上网络较好时安装是个好的选择 或参考手动安装 windows-build-tools进行安装 npm install -g node-gyp@8.3.0 linux系统 nodej...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值