El Dorado

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 304    Accepted Submission(s): 153

Problem Description

Bruce Force has gone to Las Vegas, the El Dorado for gamblers. He is interested especially in one betting game, where a machine forms a sequence of n numbers by drawing random numbers. Each player should estimate beforehand, how many increasing subsequences of length k will exist in the sequence of numbers. 


Bruce doesn't trust the Casino to count the number of increasing subsequences of length k correctly. He has asked you if you can solve this problem for him.

 

Input

The input contains several test cases. The first line of each test case contains two numbers n and k (1 ≤ k ≤ n ≤ 100), where n is the length of the sequence drawn by the machine, and k is the desired length of the increasing subsequences. The following line contains n pairwise distinct integers ai (-10000 ≤ ai ≤ 10000 ), where aiis the ith number in the sequence drawn by the machine. 

The last test case is followed by a line containing two zeros. 

Output

For each test case, print one line with the number of increasing subsequences of length k that the input sequence contains. You may assume that the inputs are chosen in such a way that this number fits into a 64 bit signed integer . 

Sample Input

10 5

1 2 3 4 5 6 7 8 9 10

3 2

3 2 1

0 0

Sample Output

252

0

Source

2008水题公开赛(比速度,OJ压力测试)

Recommend

lcy

 解题报告:这道题是一道动态规划题,题意就是求递增长度为m的总的个数,状态方程为dp[i][j] += dp[k][j - 1];其中dp[i][j]的含义:到第i个数时递增长度为j的个数;

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 110;
__int64 dp[N][N], ans;//dp[i][j]表示到第i个数的时候递增个数为j的个数
int m, n, a[N];
void DP()
{
int i, j, k;
memset(dp, 0, sizeof(dp));
for (i = 1; i <= n; ++i)
{
dp[i][1] = 1;
}
for (j = 2; j <= m; ++j)//找长度为j的增长序列
{
for (i = j; i <= n; ++i)//遍历一次
{
for (k = j -1; k < i; ++k)//比较时应该是后面的和前面的作比较k<i作为条件
{
if (a[i] > a[k])
{
dp[i][j] += dp[k][j - 1];//状态方程dp[i][j]表示第i数时,递增长度为j的个数
}
}
}
}
}
int main()
{
int i;
while (scanf("%d%d", &n, &m) != EOF && n && m)
{
memset(a, 0, sizeof(a));
for (i = 1; i <= n; ++i)
{
scanf("%d", &a[i]);
}
DP();
ans = 0;
for(i = m; i <= n; ++i)
{
ans += dp[i][m];//把递增长度为m的序列数全加起来
}
printf("%I64d\n", ans);
}
return 0;
}



 

posted on 2012-03-05 19:55  Stephen Li  阅读(227)  评论(0编辑  收藏  举报