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.
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 }
本文为博主原创文章,转载请注明出处。