Adjacent Bit Counts(uvalive)
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)的更多相关文章
- 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 ...
- BNU4286——Adjacent Bit Counts——————【dp】
Adjacent Bit Counts Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Jav ...
- POJ 3786 dp-递推 Adjacent Bit Counts *
Adjacent Bit Counts Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 599 Accepted: 502 ...
- 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 ...
- 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.输入两个 ...
- Adjacent Bit Counts(动态规划 三维的)
/** 题意: 给出一个01串 按照题目要求可以求出Fun(X)的值 比如: 111 Fun(111)的值是2: 输入: t (t组测试数据) n k (有n位01串 Fun()的值为K) 输出:有多 ...
- 河南省第六届ACM程序设计大赛
C: 最舒适的路线 (并查集) #include<cstdio> #include<cstring> #include<iostream> #include< ...
- Week__8
Monday_ 今晚补了扔鸡蛋问题的动态规划问题,补了这道题,感觉视野又开阔了些. 写了一道思维题cf 1066A 数字逻辑后半节听得打脑壳,现在很晚了,明天再看叭. Tuesday_ 今晚补了 ad ...
- UVALive 4868 Palindrometer 暴力
F - Palindrometer Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
随机推荐
- redhat7学习笔记之从零到部署javaweb项目
REDHAT7学习笔记 1. 安装vmware10 安装过程略,下载地址:链接: https://pan.baidu.com/s/16odKKkRYBxGWDVo1cz_wxA 注意,10以上版本不在 ...
- Robotframework第1课--安装RF
大家好,我是孟船长,现从事自动化测试的工作,工作用的工具就是Robotframework,现在把这“几年”的所得分享出来,希望新进入这行的朋友能够少吃点“新人苦”,能够早点入手robot framew ...
- [深度学习]实现一个博弈型的AI,从五子棋开始
好久没有写过博客了,多久,大概8年???最近重新把写作这事儿捡起来……最近在折腾AI,写个AI相关的给团队的小伙伴们看吧. 搞了这么多年的机器学习,从分类到聚类,从朴素贝叶斯到SVM,从神经网络到深度 ...
- 【转】深入 Python :Dive Into Python 中文版
原文网址:http://woodpecker.org.cn/diveintopython/power_of_introspection/lambda_functions.html 4.7. 使用 la ...
- openOffice转换的时候乱码在linux下使用openOffice的时候发现在转换后出现了乱码
openOffice转换的时候乱码 在linux下使用openOffice的时候发现在转换后出现了乱码,最后上网查了一下,按照网上的说法去试了试,最后也没有解决,也可能是我这边的linux的权限问题, ...
- apache编译参数详解
常用编译参数: ./configure //配置源代码树–prefix=/usr/local/apache //体系无关文件的顶级安装目录PREFIX ,也就Apache的安装目录.–e ...
- 5.Python使用最新爬虫工具requests-html
1.安装,在命令行输入:pip install requests-html,安装成功后,在Pycharm引入即可. 2.代码如下所示: from requests_html import HTMLSe ...
- jdk1.8新特性之函数式接口
函数式接口就是只有一个抽象方法的接口.如果这个接口里没有或者包含了两个以上的抽象方法,对不起,你不叫函数式接口,只能叫你接口.那这个函数式有啥用呢?如果配合Lambda表达式的话,可以大大的简化代码. ...
- yii2自定义500错误
由于项目想加预警监控,有一块儿是涉及到程序内部错误的500,这样的错误级别比较高,所以就需要捕获这样的错误,顺便自定义了一把视图样式 看了这篇博客,知道了如何去自定义自己错误页面 : http://t ...
- 【Leetcode 371】Sum of Two Integers
问题描述:不使用+是或-操作符进行整数的加法运算 int getSum(int a, int b); 我的思路:把整数化成二进制进行运算,注意类型是int,也就是要考虑负数.关于负数的二进制表示可见之 ...