HDU4372 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.

InputFirst 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.OutputFor 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栋楼排成一列,从左面能看到f栋楼,从右面能看到b栋楼,问楼有多少种可能的排列方式。

如果只考虑从左面看有x栋楼,相当于把n栋楼划分成x个圆排列,每个圆排列中最高的楼在该排列的最左边←这样的方案数。

如果考虑两边,那就是最高的一栋楼在中间,它左边有f-1个圆排列,右边有b-1个圆排列。

这么做的方案即是第一类斯特林数s(n-1,f-1+b-1)

然后考虑从f-1+b-1个圆排列中选出f-1个放在左边,剩下的放在右边,方案有C(f+b-2,f-1)种

 

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 using namespace std;
 7 const int mod=1e9+7;
 8 const int mxn=2011;
 9 int read(){
10     int x=0,f=1;char ch=getchar();
11     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
12     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
13     return x*f;
14 }
15 int s[mxn][mxn],c[mxn][mxn];
16 void init(){
17     for(int i=0;i<mxn;i++)c[i][0]=c[i][i]=1;
18     for(int i=0;i<mxn;i++)s[i][i]=1;
19     for(int i=1;i<mxn;i++)
20         for(int j=1;j<i;j++){
21             c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
22             s[i][j]=((long long)(i-1)*s[i-1][j]+s[i-1][j-1])%mod;
23         }
24     return;
25 }
26 int n,f,b;
27 int main(){
28     int i,j;
29     init();
30     int T=read();
31     while(T--){
32         n=read();f=read();b=read();
33         int ans=(long long)s[n-1][f+b-2]*c[f+b-2][f-1]%mod;
34         printf("%d\n",ans);
35     }
36     return 0;
37 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/6706822.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值