UVA10128
There is a queuewith N people. Every person has a different heigth. We can see P people, whenwe are looking from the beginning, and R people, when we are looking from theend. Its because they are having different height and they are covering eachother. How many different permutations of our queue has such a interestingfeature?
Input
The input consistsof T test cases. The number of them (1 ≤ T ≤ 10000) is given on the first lineof the input file. Each test case begins with a line containing a singleinteger number N that indicates the number of people in a queue (1 ≤ N ≤ 13).Then follows line containing two integers. The first integer corresponds to thenumber of people, that we can see looking from the beginning. The secondinteger corresponds to the number of people, that we can see looking from theend.
Output
For every test caseyour program has to determine one integer. Print how many permutations of Npeople we can see exactly P people from the beginning, and R people, when weare looking from the end.
Sample Input
3
10 4 4
11 3 1
3 1 2
Sample Output
90720
1026576
1
一. 题意分析
N个人排队,站在排头向后看可以看到P个人,站在排尾向前看可以看到R个人。给出N,P,R,求出所有的排队的可能情况。
二. 思路过程
假设现在队列由i-1个人变成了i个,由于谁后进到队列是无所谓的,不妨假设最矮的人是最后一个进入队列的,那么其所占的位置会有三种情况,第一种情况是站在队首,增加1个在前面能看到的人数,第二种情况是站在队尾,增加1个在后面能看到的人数,第三种情况是站在队伍中间,一共有i-2个位置可以站,但不会增加可见的人数。这样就能得到f[i][j][k]=f[i-1][j-1][k]+f[i][j][k-1]+(i-2)*f[i-1][j][k]。
三. 代码
<span style="font-family:Courier New;font-size:18px;">#include <cstdio>
#include <string.h>
#define MAXD 20
int N, P, Q;
long long int f[MAXD][MAXD][MAXD];
void ans()
{
int i, j, k;
memset(f, 0, sizeof(f));
f[1][1][1] = 1;
for(i = 2; i <= 13; i ++)
for(j = 1; j <= i; j ++)
for(k = 1; k <= i; k ++)
f[i][j][k] = f[i-1][j-1][k] + f[i-1][j][k-1] + (i-2) * f[i-1][j][k];
}
int main()
{
int t;
ans();
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d", &N, &P, &Q);
printf("%lld\n", f[N][P][Q]);
}
return 0;
}</span>