题目链接: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. va_list 简介

    原文:http://blog.sina.com.cn/s/blog_590be5290100qhxr.html va_list是一个宏,由va_start和va_end界定. typedef char ...

  2. Socks5代理Socks5 Proxy

    Socks5代理Socks5 Proxy   Socks5代理是基于Socks协议的一种代理模式.其中,5表示该协议的版本号.它可以让局域网的计算机通过socks5代理服务器,访问外网的内容.由于它工 ...

  3. Jenkins解决Host key verification failed

    1.在没有做任何操作时,是这样报错的 a.在任务中配置远程执行命令 rsync -raz --delete --progress  target/testweb-v1.1.jar  root@10.0 ...

  4. The 2016 ACM-ICPC Asia China-Final Contest Promblem D

    显然答案具有单调性,可以二分.问题是 我们二分出一个 堆数,该怎么判定能否达到这个堆数呢? 我们可以很简单的用调整法证明,最底下的一层的冰淇淋肯定是最小的那些,往上叠加的话我们再贪心的让较少的放在较小 ...

  5. Jackson对泛型的序列化和反序列化方法汇总

    说明:Jackson对于简单泛型是可以正常操作的,但是如果对于太过于复杂的泛型类有时会不成功.目前还在找着更合适的Json库.不过这一点在dotnet原生方案JavaScriptSerializer可 ...

  6. 使用和不使用navigationbar分别处理显示和返回页面

    不使用navigationbar的情况下 AnnounceViewController *pushView = [[AnnounceViewController alloc]init];pushVie ...

  7. Swift:闭包(Closures)

    一. 基本概念 闭包(Closures)是自包括的功能代码块,能够在代码中使用或者用来作为參数传值. 在Swift中的闭包与C.OC中的blocks和其他编程语言(如C#)中的lambda, java ...

  8. 使用cacheBuilder实现函数防抖

    在接口中出现的相同请求重复且连续发送的情况导致一些业务BUG,需要在接口上实现防抖 使用google的cacheBuilder import com.google.common.cache.Cache ...

  9. Solaris 目录与文件管理

    熟悉系统目录结构 掌握27个常用命令 掌握针对目录.文件的操作 掌握查找与文件内容的操作 一.命令 命令:内部命令(不依赖其他文件,可以直接执行)与外部命令 .他是用于实现某一类功能的指令或程序,其执 ...

  10. Intel平台map