题目链接:https://vjudge.net/problem/HDU-4372

Count the Buildings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2509    Accepted Submission(s): 815

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
 
Source

题意:

有n幢高度不一的楼房位于一条直线上,问有多少种方案数,使得人从第一幢往前看时看到f幢,从最后一幢往后看时看到b幢?

题解:

1.可知最高的那栋必定能够看到,于是就分成了左边和右边。

2.对于左边的楼而言,需要把他们分成f-1组,每一组的最高楼在最左边,这样就把组内其他的楼遮住了,于是就看到f-1栋。对于右边的也如此。

3.那怎么分组呢?首先,求出求出第一类斯特林数 S[n-1][f-1+b-1],即把除了最高楼之外的楼房排成f-1+b-1个圈。由于每一组中最高的楼房固定在左边或右边,这样就对应了圈。换句话说,对排列好的一个圈选定一栋楼房,而这栋楼房就是最高的那栋,然后再把这个圈展开成一列,这样就对应了一组。所以可以用第一类斯特林数求出分组的方案数。

4.分好组后,就直接从 f+b-2组中抽取f-1组放在左边(由于要求递增,所以选出来之后他们的位置就固定了,不需要再排列),总有 C[f+b-2][f-1]种选择。

5.综上,总共有 S[n-1][f-1+b-1] * C[f+b-2][f-1] 种方案数。注意, 当n-1<f+b-2时, 问题无解。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e3+; LL S[MAXN][MAXN], C[MAXN][MAXN]; void init()
{
for(int i = ; i<MAXN; i++)
{
C[i][] = ;
for(int j = ; j<=i; j++)
C[i][j] = (C[i-][j-]+C[i-][j])%MOD;
} memset(S, , sizeof(S));
for(int i = ; i<MAXN; i++)
{
S[i][] = ; S[i][i] = ;
for(int j = ; j<i; j++)
S[i][j] = (((i-)*S[i-][j])%MOD + S[i-][j-])%MOD;
}
} int main()
{
init();
int T, n, f, b;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d", &n, &f, &b);
LL ans;
if(n-<f+b-) ans = ;
else ans = (1LL*S[n-][f+b-]*C[f+b-][f-])%MOD;
printf("%lld\n", ans);
}
}

HDU4372 Count the Buildings —— 组合数 + 第一类斯特林数的更多相关文章

  1. 【HDU4372】Count the Buildings (第一类斯特林数)

    Description $N$座高楼,高度均不同且为$1~N$中的数,从前向后看能看到$F$个,从后向前看能看到$B$个,问有多少种可能的排列数. $T$组询问,答案模$1000000007$.其中$ ...

  2. hdu 4372 Count the Buildings —— 思路+第一类斯特林数

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4372 首先,最高的会被看见: 然后考虑剩下 \( x+y-2 \) 个被看见的,每个带了一群被它挡住的楼, ...

  3. HDU 4372 Count the Buildings:第一类Stirling数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4372 题意: 有n栋高楼横着排成一排,各自的高度为1到n的一个排列. 从左边看可以看到f栋楼,从右边看 ...

  4. 【HDU 4372】 Count the Buildings (第一类斯特林数)

    Count the Buildings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  5. HDU4372 Count the Buildings (+题解:斯特林数)

    题面 (笔者翻译) There are N buildings standing in a straight line in the City, numbered from 1 to N. The h ...

  6. HDU 4372 Count the Buildings——第一类斯特林数

    题目大意:n幢楼,从左边能看见f幢楼,右边能看见b幢楼 楼高是1~n的排列. 问楼的可能情况 把握看到楼的本质! 最高的一定能看见! 计数问题要向组合数学或者dp靠拢.但是这个题询问又很多,难以dp ...

  7. 洛谷P4609 [FJOI2016]建筑师(第一类斯特林数+组合数)

    题面 洛谷 题解 (图片来源于网络,侵删) 以最高的柱子\(n\)为分界线,我们将左边的一个柱子和它右边的省略号看作一个圆排列,右边的一个柱子和它左边的省略号看作一个圆排列,于是,除了中间的最高的柱子 ...

  8. 【2019雅礼集训】【CF 960G】【第一类斯特林数】【NTT&多项式】permutation

    目录 题意 输入格式 输出格式 思路 代码 题意 找有多少个长度为n的排列,使得从左往右数,有a个元素比之前的所有数字都大,从右往左数,有b个元素比之后的所有数字都大. n<=2*10^5,a, ...

  9. 【CF715E】Complete the Permutations(容斥,第一类斯特林数)

    [CF715E]Complete the Permutations(容斥,第一类斯特林数) 题面 CF 洛谷 给定两个排列\(p,q\),但是其中有些位置未知,用\(0\)表示. 现在让你补全两个排列 ...

随机推荐

  1. chpasswd、dd命令、find实战、添加系统服务、buffer、cached

    1.如果两个文件的每一行想一一对应 paste 1.txt 2.txt # 文件3.txt中存放着用户跟密码,想要添加用户并设置密码: # 用户必须存在,文件格式必须是--用户名:密码 chpassw ...

  2. luogu P2066 机器分配

    题目背景 无 题目描述 总公司拥有高效设备M台,准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M≤15 ...

  3. springboot 启动类启动跳转到前端网页404问题的两个解决方案

    前段时间研究springboot 发现使用Application类启动的话, 可以进入Controller方法并且返回数据,但是不能跳转到WEB-INF目录下网页, 前置配置 server: port ...

  4. Java实验--关于英文短语词语接龙

    在课堂上经过实验之后,重新在宿舍里面从0开始编写大概30分钟左右能够完成这个实验,不是原来的思路. 该实验的表述为:从两个文本input1.txt和input2.txt中读取英文单词,若前面的英文单词 ...

  5. 继承LIst 的类JSON序列化,无法序列化属性的问题

    /// <summary> /// Paged list /// </summary> /// <typeparam name="T">T< ...

  6. 邁向IT專家成功之路的三十則鐵律 鐵律二十二:IT人升遷之道-無為

    升遷管道是許多人求職時相當重要的考量之一,畢竟人除了很愛錢之外更愛顯赫的頭銜,然而在企業中越顯赫的頭銜,其背後通常有更多的罵名,因為許多人的高官厚爵都是踩著一群人的頭頂爬上去的,隨時哪一天跌了下來,都 ...

  7. golang 版本升降之后报错——imports runtime: C source files not allowed when not using cgo or SWIG

    问题: golang 升级或者降级版本之后,执行编译报错如下: package github.com/onsi/ginkgo/ginkgo imports runtime: C source file ...

  8. iOS -- SKScene类

      SKScene类 继承自 SKEffectNode:SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject ...

  9. win7 32位安装pyqt

    参考 http://blog.csdn.net/fairyeye/article/details/6607981 http://www.cnblogs.com/toSeek/p/6363036.htm ...

  10. NMM3DViewer 设计

    在FrameworkInterfaces工程的INMM3DServer.cs中定义了 岩石材料结构 BlockMaterial  -----> StrBLOCKProperty     publ ...