UVa 10721 - Bar Codes

題目:給你n個連續排列的長條形單元,單元可以使黑色或或者白色,相鄰的桶重顏色看成是一個部分;

問把n個單元分成k個部分,每個部分都不超過m的分法數。

分析:動態規劃,dp。利用動態規劃建模,找到最後加入的數字和前面集合的關係。

定義狀態:f(i,j,k)為i個單元分成j部分,每部分不超過k個分法數;

轉移方程:f(i,j,k)= sum(f(i-p,j-1,k)) { 1 ≤ p ≤ k};

說明:數據較大哦,注意使用long long防止溢出╮(╯▽╰)╭。

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

using namespace std;

long long f[55][55][55];
//f(i. j, k) = sum( f(i-p, j-1,  k)  p = 1..k ) 

int main()
{
	for (int i = 0; i < 51; ++ i)
	for (int j = 0; j < 51; ++ j)
	for (int k = 0; k < 51; ++ k)
		f[i][j][k] = 0LL;

	for (int i = 0; i < 51; ++ i)
		f[0][0][i] = 1LL;
	for (int i = 1; i < 51; ++ i)
	for (int j = 1; j < 51; ++ j)
	for (int k = 1; k < 51; ++ k)
	for (int p = 1; p <= k && p <= i; ++ p)
		f[i][j][k] += f[i-p][j-1][k];

	int n,k,m;
	while (cin >> n >> k >> m)
		cout << f[n][k][m] << endl;

    return 0;
}
时间: 2024-10-11 12:38:38

UVa 10721 - Bar Codes的相关文章

UVA - 146 - ID Codes (枚举排列)

UVA - 146 ID Codes Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exercise greater control over its ci

Brute Force &amp; STL --- UVA 146 ID Codes

 ID Codes  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=3&problem=82&mosmsg=Submission+received+with+ID+14418598 Mean: 求出可重排列的下一个排列. analyse: 直接用STL来实现就可.自己手动写了一个,并不复杂.

UVA10721 - Bar Codes(DP)

题目链接 题目大意:给你n, k, m, 要求满足BC(n,k,m)bar code的数目.n表示这样的字符串长度为n,k表示相同颜色的段落有k段,m代表每段内最多只能有m个相同的. 解题思路:f[n][k][m]:表示第i个位置的字符,现在已经形成了k段,长度为m. f[n][k][m] = f[n + 1][k][m + 1] + f[n + 1][k + 1][1]; 代码: #include <cstdio> #include <cstring> const int max

UVA 146 ID Codes(下一个排列)

C - ID Codes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-05-12) Description  ID Codes  It is 2084 and the year of Big Brother has finally arrived, albeit a century l

LightOJ1191 Bar Codes(DP)

题目大概是,二进制数可以看作是由几段连续的0和连续的1组成,问:n位没有前导0的 且 共用k段连续0/1组成的 且 连续0/1个数不超过m的二进制数有多少个. 用dp[n][k][m]表示问题 dp[i][1][j]=1 (i<=j) 通过枚举第一段连续数字的个数first,使dp[n][k][m]从dp[n-first][k-1][m]转移,具体就是dp[n][k][m]=∑dp[n-i][k-1][m] (1<=i<=m) 1 #include<cstdio> 2 #in

UVA 146 ID Codes

求用这些字母的下一个排列是什么.直接使用C++ STL库里面的next_permutation #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { char s[1000]; while (~scanf("%s", s)) { if (strcmp(s, "#") == 0) break; int y =

uva 146 ID Codes(求下一个排列)水水水

分别利用STL中的next_permutation和它的实现原理来写: next_permutation: <span style="font-family:Courier New;font-size:18px;">#include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> using namespace std; int main()

dp题目列表

10271 - Chopsticks 10739 - String to Palindrome 10453 - Make Palindrome 10401 - Injured Queen Problem 825 - Walking on the Safe Side 10617 - Again Palindrome 10201 - Adventures in Moving - Part IV 11258 - String Partition 10564 - Paths through the Ho

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes