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

x1 ∗ x2 + x2 ∗ x3 + x3 ∗ x4 + . . . + xn−1 ∗ xn

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
题解:求一个长度为p,构造一个价值为n的字符串的方法数;

首先如果我们要构造一个价值为7的字符串,若分为两部分的话,我们有1+6,2+5,3+4等3种方案,对于1+6,我们又可以分为一个子问题,将6分为两部分,以此类推;

这样就满足一个最优子结构;这样我们把原问题可以分为若干子问题,我们用的dp[i][j]表示长度为i-1,价值为j的字符串的方法数,由于最后一位0与1两种情况,我们可以定义3维数组;

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];

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long ll;
const int MAXN=1e3+;
int m,n;
int dp[MAXN][MAXN][];
using namespace std;
int main()
{
cin>>m;
int p,k,u;
while(m--)
{
cin>>p>>k>>u;
memset(dp,,sizeof(dp));
dp[][][]=;
dp[][][]=;
for(int i=;i<=k;i++)
{
for(int j=;j<=u;j++)
{
for(int t=;t<;t++)
{
if(t==)
{
dp[i][j][t]=dp[i-][j][]+dp[i-][j][];
}
else
{
dp[i][j][t]=dp[i-][j][]+dp[i-][j-][];
}
}
}
}
cout<<p<<" "<<dp[k][u][]+dp[k][u][]<<endl;
}
}

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

Adjacent Bit Counts(uvalive)的更多相关文章

  1. Adjacent Bit Counts(01组合数)

    Adjacent Bit Counts 4557 Adjacent Bit CountsFor a string of n bits x 1 , x 2 , x 3 ,..., x n , the a ...

  2. BNU4286——Adjacent Bit Counts——————【dp】

    Adjacent Bit Counts Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Jav ...

  3. POJ 3786 dp-递推 Adjacent Bit Counts *

    Adjacent Bit Counts Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 599   Accepted: 502 ...

  4. nyoj 715 Adjacent Bit Counts

    描述 For a string of n bits x1, x2, x3, …, xn,  the adjacent bit count of the string  is given by      ...

  5. POJ 3786 Adjacent Bit Counts (DP)

    点我看题目 题意 :给你一串由1和0组成的长度为n的数串a1,a2,a3,a4.....an,定义一个操作为AdjBC(a) = a1*a2+a2*a3+a3*a4+....+an-1*an.输入两个 ...

  6. Adjacent Bit Counts(动态规划 三维的)

    /** 题意: 给出一个01串 按照题目要求可以求出Fun(X)的值 比如: 111 Fun(111)的值是2: 输入: t (t组测试数据) n k (有n位01串 Fun()的值为K) 输出:有多 ...

  7. 河南省第六届ACM程序设计大赛

    C:  最舒适的路线 (并查集) #include<cstdio> #include<cstring> #include<iostream> #include< ...

  8. Week__8

    Monday_ 今晚补了扔鸡蛋问题的动态规划问题,补了这道题,感觉视野又开阔了些. 写了一道思维题cf 1066A 数字逻辑后半节听得打脑壳,现在很晚了,明天再看叭. Tuesday_ 今晚补了 ad ...

  9. UVALive 4868 Palindrometer 暴力

    F - Palindrometer Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

随机推荐

  1. CF1117D Magic Gems

    CF1117D Magic Gems 考虑 \(dp\) , \(f[i]\) 表示用 \(i\) 个单位空间的方案数,答案即为 \(f[n]\). 对于一个位置,我们可以放 \(Magic\) 的, ...

  2. iphone——日期处理

    http://blog.csdn.net/lingedeng/article/details/6996599 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功 ...

  3. 《DSP using MATLAB》第7章开始 Example7.1

    FIR低通滤波器的设计要求示意图:

  4. Centos用yum升级mysql到(5.5.37) (转)

    http://www.cnblogs.com/ikodota/p/use_yum_update_mysql.html 1. Change root user su - ## OR ## sudo -i ...

  5. 相关TableLayoutPanel分页显示自定义控件

    public partial class AcrossGrid : UserControl { /// <summary> /// 一页数量 /// </summary> ; ...

  6. win10下启动zkui

    zkui是一个开源的zookeeper可视化工具,现在看下我们怎么启动这个工具.首先下载源码(我把它放在E:\workspace): git clone https://github.com/Deem ...

  7. mysql存储引擎之myisam学习

    myisam存储引擎特点:1.不支持事务2.表级锁定(更新时锁整个表,其索引机制是表级索引,这虽然可以让锁定的实现成本很小,但是也同时大大降低 了其并发性能) 3.读写互相阻塞:不仅会在写入的时候阻塞 ...

  8. 结合示例说明C++中const和指针结合时怎么理解

    在之前随笔<C++中const使用要点(一)>中简单叙述了const int*.int* const和const int* const的区别,记住三句话就能在实际运用时用对,但是看书时发现 ...

  9. Arduino+A4988驱动两相四线步进电机

    先吐槽一下,在某宝买东西这么多年碰到的不靠谱的卖家也没这几天多.丝杆发短,42电机只有32大,碳杆上的鱼眼粘的没法再歪了还死紧……所以组装还得几天.于是先玩了一下DC-DC降压模块和A4988,规划了 ...

  10. 双口RAM,值得研究

    在FPGA设计过程中,使用好双口RAM,也是提高效率的一种方法. 官方将双口RAM分为简单双口RAM和真双口RAM. 简单双口RAM只有一个写端口,一个读端口. 真双口RAM分别有两个写端口和两个读端 ...