题目链接: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. ubuntu和raspberry下调试python_spi备忘

    Ubuntu12.04 自安装python3.3中头文件Python.h路径:usr/local/python3.3/include/python3.3m Ubuntu12.04 自带的Python2 ...

  2. Nginx三种模式的虚拟主机(附Apache基于域名的虚拟主机)

    1.安装nginx # pcre中文"perl兼容正则表达式",安装pcre库是为了让nginx支持具备URL重写功能 # 的Rewrite模块,rewrite可以实现动态页面转成 ...

  3. DELPHI 10.2 TOKYO搭建LINUX MYSQL开发环境

    DELPHI 10.2 TOKYO搭建LINUX MYSQL开发环境 笔者使用ubuntu64位LINUX 首先必须保证LINUX可以连互联网. 安装MYSQLsudo apt-get update ...

  4. Android Framework 记录之一

    简介 之前的研究太偏向应用层功能实现了,很多原理不了解没有深究,现在研究framework框架层了. 记录 1.下载源码,目录如下: 2.Android系统的层次如下: 3.项目目录简单分析如下: 4 ...

  5. GOPATH设置

    go help gopath查看gopath的原文帮助信息 go env查看gopath的配置 GOPATH与工作空间 前面我们在安装Go的时候看到需要设置GOPATH变量,Go从1.1版本到1.7必 ...

  6. 【转载】GitHub中国区前100名到底是什么样的人

    转载了这篇文章: http://www.jianshu.com/p/d29cba7934c9 这篇文章真是太牛了!转载过来涨涨见识,同时好好励志一把.还有,ruanyifeng怎么长那样... 哈 另 ...

  7. 转: 多版本并发控制(MVCC)在分布式系统中的应用 (from coolshell)

    from:  http://coolshell.cn/articles/6790.html 问题 最近项目中遇到了一个分布式系统的并发控制问题.该问题可以抽象为:某分布式系统由一个数据中心D和若干业务 ...

  8. C# Ftp方式下载文件(无用户认证方式,支持断点续传)

    类代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  9. Odoo10尝鲜:MRP 10 新概念

    OEE [ overall equipment Effectiveness 整体设备效率 ] 整體設備效率是整合稼働率 (Availability).產能效率 (Performance).良率 (Qu ...

  10. Solaris 目录与文件管理

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