NYOJ - 715 - Adjacent Bit Counts --第六届河南省程序设计大赛 (DP!!)

Adjacent Bit Counts

时间限制:1000 ms  |  内存限制:65535 KB

难度:4

描述

For a string of n bits x1, x2, x3, …, xn,  the adjacent bit count of the string  is given by     fun(x) = x1*x2 + x2*x3 + x3*x 4 + … + xn-1*x n

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

Fun(011101101) = 3

Fun(111101101) = 4

Fun (010101010) = 0

Write a program which takes as input integers n and p and returns the number of bit strings x of n bits (out of 2?) that satisfy  Fun(x) = p.

For example, for 5 bit strings, there are 6 ways of getting fun(x) = 2:

11100, 01110, 00111, 10111, 11101, 11011

输入
On the first line of the input is a single positive integer k, telling the number of test cases to follow. 1 ≤ k ≤ 10 Each case is a single line that contains a decimal integer giving the number (n) of bits in the bit strings, followed by a single space,
followed by a decimal integer (p) giving the desired adjacent bit count. 1 ≤ n , p ≤ 100
输出
For each test case, output a line with the number of n-bit strings with adjacent bit count equal to p.
样例输入
2
5 2
20 8 
样例输出
663426
来源
第六届河南省程序设计大赛

简单DP题。。

定义数组dp[105][105][2];其中dp[i][j][0]代表第j位是0的时候长度为j的串组成值为i的串的种数,dp[i][j][1]代表

j位是1的时候长度为j的串组成值为i的串的种数,

容易看出dp[i][j][0]=dp[i][j-1][0]+dp[i][j-1][1];   dp[i][j][1]=dp[i-1][j-1][1]+dp[i][j-1][0];

所以,初始化先把f的值全部赋成0.然后dp[0][1][0]=dp[0][1][1]=1;然后再求出所有dp[0][j][0]和dp[0][j][1]的值;

之后两个for循环就ok了!

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

long long dp[105][105][2];

void init()
{
	memset(dp, 0, sizeof(dp));
	dp[0][1][0] = 1;
	dp[0][1][1] = 1;
	for(int i=2; i<=100; i++)
	{
		dp[0][i][0] = dp[0][i-1][0] + dp[0][i-1][1];
		dp[0][i][1] = dp[0][i-1][0];
	}
	for(int i=1; i<=100; i++)
		for(int j=2; j<=100; j++)
		{
			dp[i][j][0] = dp[i][j-1][1] + dp[i][j-1][0];
			dp[i][j][1] = dp[i-1][j-1][1] + dp[i][j-1][0];
		}
}

int main()
{
	init();
	int k, n, p;
	scanf("%d", &k);
	while(k--)
	{
		scanf("%d %d", &n, &p);
		printf("%I64d\n", dp[p][n][0]+dp[p][n][1]);
	}
	return 0;
} 
时间: 2024-12-24 03:07:35

NYOJ - 715 - Adjacent Bit Counts --第六届河南省程序设计大赛 (DP!!)的相关文章

nyoj 711最舒适的路线(第六届河南省程序设计大赛 并查集)

最舒适的路线 时间限制:5000 ms  |  内存限制:65535 KB 难度:5 描述 异形卵潜伏在某区域的一个神经网络中.其网络共有N个神经元(编号为1,2,3,-,N),这些神经元由M条通道连接着.两个神经元之间可能有多条通道.异形卵可以在这些通道上来回游动,但在神经网络中任一条通道的游动速度必须是一定的.当然异形卵不希望从一条通道游动到另一条通道速度变化太大,否则它会很不舒服. 现在异形卵聚居在神经元S点,想游动到神经元T点.它希望选择一条游动过程中通道最大速度与最小速度比尽可能小的路

NYOJ - 716 - River Crossing --第六届河南省程序设计大赛 (简单DP!!)

River Crossing 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Afandi is herding N sheep across the expanses of grassland  when he finds himself blocked by a river. A single raft is available for transportation. Afandi knows that he must ride on the raft for

nyoj 712 探 寻 宝 藏(双线dp 第六届河南省程序设计大赛)

探 寻 宝 藏 时间限制:1000 ms  |  内存限制:65535 KB 难度:5 描述 传说HMH大沙漠中有一个M*N迷宫,里面藏有许多宝物.某天,Dr.Kong找到了迷宫的地图,他发现迷宫内处处有宝物,最珍贵的宝物就藏在右下角,迷宫的进出口在左上角.当然,迷宫中的通路不是平坦的,到处都是陷阱.Dr.Kong决定让他的机器人卡多去探险. 但机器人卡多从左上角走到右下角时,只会向下走或者向右走.从右下角往回走到左上角时,只会向上走或者向左走,而且卡多不走回头路.(即:一个点最多经过一次).当

nyoj714 Card Trick(第六届河南省程序设计大赛)

题目714 题目信息 运行结果 本题排行 讨论区 Card Trick 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 The magician shuffles a small pack of cards, holds it face down and performs the following procedure: The top card is moved to the bottom of the pack. The new top card is deal

NYOJ 715 Adjacent Bit Counts

 Adjacent Bit Counts 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 For a string of n bits x1, x2, x3, -, xn,  the adjacent bit count of the string  is given by     fun(x) = x1*x2 + x2*x3 + x3*x 4 + - + xn-1*x n which counts the number of times a 1 bit is

nyoj1254 Code the Tree (第七届河南省程序设计大赛)

题目1254 题目信息 运行结果 本题排行 讨论区 Code the Tree 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code of such a tree is built as follows: the

nyoj1253 Turing equation(第七届河南省程序设计大赛)

题目1253 题目信息 运行结果 本题排行 讨论区 Turing equation 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 The fight goes on, whether to store  numbers starting with their most significant digit or their least  significant digit. Sometimes  this  is also called  the  "Endian

nyoj1248 海岛争霸(第七届河南省程序设计大赛)

题目1248 题目信息 运行结果 本题排行 讨论区 海岛争霸 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 神秘的海洋,惊险的探险之路,打捞海底宝藏,激烈的海战,海盗劫富等等.加勒比海盗,你知道吧?杰克船长驾驶着自己的的战船黑珍珠1号要征服各个海岛的海盜,最后成为海盗王. 这是一个由海洋.岛屿和海盗组成的危险世界.杰克船长准备从自己所占领的岛屿A开始征程,逐个去占领每一个岛屿.面对危险重重的海洋与诡谲的对手,如何凭借智慧与运气,建立起一个强大的海盗帝国. 杰克船长

nyoj1255 Rectangles(第七届河南省程序设计大赛)

题目1255 题目信息 运行结果 本题排行 讨论区 Rectangles 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 Given N (4 <= N <= 100)  rectangles and the lengths of their sides ( integers in the range 1..1,000), write a program that finds the maximum K for which there is a sequence