10721-bar Codes
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1662
A Bar-code symbol consists of alternating dark and light bars, starting with a dark bar on the left. Each bar is a number of units wide. Figure 1 shows a bar-code symbol consisting of 4 bars that extend over 1+2+3+1=7 units.
Figure 1:bar-code over 7 units with 4 bars
In general, the bar code BC (n,k,m) are the set of all symbols with k bars, together extend over exactly n units, each B Ar being at most m units wide. For instance, the symbol in Figure 1 belongs to BC (7,4,3) but not to BC (7,4,2). Figure 2 shows symbols in BC (7,4,3). Each ' 1 ' represents a dark unit, each ' 0 ' a light unit.
0:1.0001 million | 4:1,001,110 | 8:1.1001 million | 12:1,101,110
1:1,000,110 | 5:1.011 million | 9:1,100,110 | 13:1,110,010
2:1.001 million | 6:1.0111 million | 10:1.101 million | 14:1.1101 million
3:1.0011 million | 7:1,100,010 | 11:1.1011 million | 15:1,110,110
Figure 2:all Symbols of BC (7,4,3)
Input
Each input would contain three positive integers n, K, and M (1≤n, K, m≤50).
Output
For each input print the total number of symbols in BC (n,k,m). Output'll fit in 64-bit signed integer.
Train of thought: we can set DP[I][J] to indicate the number of cases with the I bar,j unit, then dp[i][j]=sum{dp[i-1][j-k]},1<=k<=m.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Complete code:
01./*0.016s*/
.
#include <cstdio>
#include <cstring>
05.const int maxd =;
07.long long dp[maxd][maxd];
09.int Main ()
10.{ int N, K, M, I, J, K; while (~scanf ("%d%d%d", &n, &k, &m))
. { memset (DP, 0, sizeof (DP)); for (i = 1; I <= M && i <= N; ++i) Dp[1][i] = 1;///Only one contiguous block must be only one
. for (i = 2; I <= K; ++i)
. for (j = i; j <= N; ++j)
. for (k = 1; k < J && K <= M; ++k)
. DP[I][J] + = dp[i-1][j-k]; printf ("%lld\n", Dp[k][n]);
. return 0;
23.}