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.
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.
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
大致题意:有一系列的楼房,高度从1~n,然后从左侧看能看到f个楼房,右侧看能看到b个楼房,问有多少个方案数满足。
思路:高度为n的那幢楼从两边看一定都能看到,然后左边还需看到f-1个楼,右边还需要看到b-1个楼,类比于左边还需要有f-1个环排列,右边还需要b-1个环排列,所以我们先求出n-1个楼分成f-1+b-1个环排列的方案数,然后再从f-1+b-1个环排列中选出f-1个环排列放在高度为n的楼的左边,所以总共的方案数为S1(n-1,f-1+b-1)*C(f-1+b-1,f-1)%mod
代码如下
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
const int mod=1e9+7;
using namespace std;
ll stir[2005][2005];
ll c[2005][2005];//组合数
void init(){
c[0][0]=1;
stir[0][0]=1;
for(int i=1;i<=2000;i++)
{
c[i][0]=c[i][i]=1;
stir[i][0]=0;
stir[i][i]=1;
for(int j=1;j<i;j++)
{
c[i][j] = (c[i-1][j] + c[i-1][j-1])%mod;
stir[i][j]=(stir[i-1][j-1]+stir[i-1][j]*(i-1))%mod;
}
}
}
int main ()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
int n,f,b;
scanf("%d%d%d",&n,&f,&b);
if(f+b-2>n)
printf("0\n");
else
printf("%lld\n",(c[f+b-2][f-1]*stir[n-1][b+f-2])%mod);
}
return 0;
}