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 }
时间: 2024-10-13 16:01:55

HDU4372 Count the Buildings的相关文章

HDU 4372 Count the Buildings(组合数学-斯特林数,组合数学-排列组合)

Count the Buildings 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

HDOJ 题目4372 Count the Buildings(斯特林第一类数)

Count the Buildings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 738    Accepted Submission(s): 246 Problem Description There are N buildings standing in a straight line in the City, numbere

Count the Buildings

K - Count the Buildings 参考:Count the Buildings 思路可以借鉴,但是代码略有问题 写的时候 re 了 9 发,然后把变量定义的顺序换了一下居然 A 了,以为这个是个骚操作,最后才发现是真的会越界,当 f+b>n+2 的时候就有可能会发生越界,而这种情况,if 判断一下就好 代码: // Created by CAD on 2019/8/17. #include <bits/stdc++.h> using namespace std; using

HDU 4372 Count the Buildings:第一类Stirling数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4372 题意: 有n栋高楼横着排成一排,各自的高度为1到n的一个排列. 从左边看可以看到f栋楼,从右边看可以看到b栋楼,并且高的楼会挡住低的楼. 问你这些楼有多少种排列方法. 题解: 由于高的楼会挡住低的楼,所以这些楼首先会被划分成f+b-2个区域(除去中间最高的楼),并且左边有f-1个,右边有b-1个. 对于一个区域(假设在左边),这个区域由若干栋楼组成,并且最高的楼一定在最左边. 那么,由一个区域

HDU 4372 Count the Buildings(组合数+斯特林数)

 题意:N座高楼,高度均不同且为1-N中的数,从前向后看能看到F个,从后向前看能看到B个,问有多少种可能的排列数. 思路:一开始想的是dp,但是数据范围达到2000,空间复杂度无法承受. 考虑以最高的楼分界,左边有f-1个递增的楼,右边有b-1个递减的楼,考虑将剩下n-1楼分为f+b-2组,规定每组中最高的在最左边,那么只需要 再从n-1组选出f-1组放到左边即可. 现在解决将n-1个楼分为f+b-2组每组最高的楼在左边的这个问题,这等价于将n-1个楼分为f+b-2个环的排列数,因为长度为n

HDU4372-Count the Buildings(第一类Stirling数+组合计数)

Count the Buildings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 528    Accepted Submission(s): 171 Problem Description There are N buildings standing in a straight line in the City, numbere

暑假集训-合训第9场

ID Origin Title   8 / 22 Problem A HDU 4358 Boring counting 35 / 52 Problem B HDU 4359 Easy Tree DP? 31 / 79 Problem C HDU 4362 Dragon Ball   1 / 2 Problem D HDU 4363 Draw and paint 18 / 56 Problem E HDU 4365 Palindrome graph   3 / 17 Problem F HDU 4

Firebug Console API

原文发布时间为:2011-06-06 -- 来源于本人的百度文章 [由搬家工具导入] Console API 当打开 firebug (也包括 Chrome 等浏览器的自带调试工具),window 下面会注册一个叫做 console 的对象,它提供多种方法向控制台输出信息,供开发人员调试使用。下面是这些方法的一个简单介绍,适时地运用它们,对于提高开发效率很有帮助。 console.log(object[, object, ...]) 使用频率最高的一条语句:向控制台输出一条消息。支持 C 语言

[CareerCup] 15.2 Renting Apartment II 租房之二

Write a SQL query to get a list of all buildings and the number of open requests (Requests in which status equals 'Open'). -- TABLE Apartments +-------+------------+------------+ | AptID | UnitNumber | BuildingID | +-------+------------+------------+