Count the Buildings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2463 Accepted Submission(s): 806
Problem Description
There are N buildings standing in a straight line in the City, numbered from 1 to N. The heights of all the buildings are distinct and between 1 and N. You can see F buildings when you standing in front of the first building and looking forward, and B buildings
when you are behind the last building and looking backward. A building can be seen if the building is higher than any building between you and it.
Now, given N, F, B, your task is to figure out how many ways all the buildings can be.
Now, given N, F, B, your task is to figure out how many ways all the buildings can be.
Input
First line of the input is a single integer T (T<=100000), indicating there are T test cases followed.
Next T lines, each line consists of three integer N, F, B, (0<N, F, B<=2000) described above.
Next T lines, each line consists of three integer N, F, B, (0<N, F, B<=2000) described above.
Output
For each case, you should output the number of ways mod 1000000007(1e9+7).
Sample Input
2 3 2 2 3 2 1
Sample Output
2 1
/*
题意:一排房子,总共N幢,高度为1~N,从左边能看到F幢,从右边能看到F幢,问房子排列的可能
思路:转换为第一类Stirling数,最高一幢必然能看到,则其左边能看到的为F-1幢,右边为B-1幢,
这样可以把问题转换为先把N-1幢房子分作B+F-2个环排列,再把这B+F-2个环排列在最高楼两侧分布
的情况,即s[N-1][F+B-2]*c[F+B-2][F-1],注意 B+F-1大于N的情况为0
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define maxn 2010
typedef long long LL;
LL s[maxn][maxn];//存放要求的第一类Stirling数
LL c[maxn][maxn];//存放组合数
const LL mod=1e9+7;//取模
void init()//预处理
{
memset(s,0,sizeof(s));
s[1][1]=1;
for(int i=2;i<=maxn-1;i++)
{
for(int j=1;j<=i;j++)
{
s[i][j]=s[i-1][j-1]+(i-1)*s[i-1][j];
if(s[i][j]>=mod)
s[i][j]%=mod;
}
}
memset(c,0,sizeof(c));
c[0][0] = 1;
for(int i=1;i<maxn;i++)
{
c[i][0] = 1;
for(int j=1;j<=i;j++)
c[i][j] = (c[i-1][j]+c[i-1][j-1])%mod;
}
}
int main()
{
init();
int t;
int N,F,B;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&N,&F,&B);
if(F+B-1<=N)
printf("%lld\n",(s[N-1][F+B-2]*c[F+B-2][F-1])%mod);
else //注意处理不存在的情况
printf("0\n");
}
}