2015
05-24

# Count the Buildings

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.

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.

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.

2
3 2 2
3 2 1

2
1

0 < N, F, B <= 2000

ans(n, f, b) = C[f + b - 2][f - 1] * S[n - 1][f + b - 2];

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
typedef long long LL;

const int N=2005;
const LL MOD=1000000007;

LL C[N][N];
LL S[N][N];

void Init()
{
int i,j;
for(i=0;i<N;i++)
{
C[i][0]=1;
C[i][i]=1;
S[i][0]=0;
S[i][i]=1;
for(j=1;j<i;j++)
{
C[i][j]=(C[i-1][j]%MOD+C[i-1][j-1]%MOD)%MOD;
S[i][j]=((i-1)%MOD*S[i-1][j]%MOD+S[i-1][j-1]%MOD);
}
}
}

int main()
{
LL t,n,f,b,ans;
Init();
scanf("%I64d",&t);
while(t--)
{
scanf("%I64d%I64d%I64d",&n,&f,&b);
ans=C[f+b-2][f-1]%MOD*S[n-1][f+b-2]%MOD;
printf("%I64d\n",ans);
}
return 0;
}