[Hdu4372] Count the Buildings
[Hdu4372] Count the Buildings
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
The 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
试题分析
由于我们求的是连续最长上升与倒序连续最长上升,所以这两个序列肯定以n为分界,把n拿出来单独考虑。
所以A变成A-1,B变成B-1。
然后考虑把这个序列看成一段一段的,用最长连续上升&最长连续倒序上升序列中的元素看成分界,那么段中剩下的元素可以随便换,设段长为\(x\),那么其中便能换\((x-1)!\)种方案。
\((x-1)!\),这不是把\((x-1)\)个元素分成一个轮换的方案数么?每次我们只需要把这个轮换中的最大的拿出来当做头部就可以了。
于是分成A-1+B-1组的方案就是:$$
\begin{bmatrix}
n-1 \ A+B-2
\end{bmatrix}$$
这A-1+B-1组还有放左边和右边的方案,也就是:$$\binom{A+B-2}{A-1}$$
由于左边和右边分组确定后顺序是按照组内最大值排序的,所以顺序是唯一的,无需考虑。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
#define LL long long
inline LL read(){
LL x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const LL INF = 2147483600;
const LL MAXN = 4010;
const LL Mod = 1000000007;
LL N,A,B,T; LL fac[MAXN+1],ifac[MAXN+1],inv[MAXN+1];
LL f[MAXN+1][MAXN+1];
inline void init(){
fac[0]=1; ifac[1]=1; inv[1]=1; ifac[0]=1;
for(LL i=1;i<=MAXN;i++) fac[i]=fac[i-1]*i%Mod;
for(LL i=2;i<=MAXN;i++)
inv[i]=(Mod-(Mod/i))*inv[Mod%i]%Mod,ifac[i]=ifac[i-1]*inv[i]%Mod;
f[0][0]=1; for(LL i=1;i<=MAXN;i++){
for(LL j=1;j<=MAXN;j++)
f[i][j]=((i-1)*f[i-1][j]%Mod+f[i-1][j-1])%Mod;
} return ;
}
inline LL C(LL n,LL m){
if(n<m) return 0; if(!m) return 1;
return fac[n]*ifac[n-m]%Mod*ifac[m]%Mod;
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
T=read(); init();
while(T--){
N=read(),A=read(),B=read(); //cout<<f[N-1][A+B-2]<<endl;
printf("%lld\n",f[N-1][A+B-2]*C(A+B-2,A-1)%Mod);
}
return 0;
}
[Hdu4372] Count the Buildings的更多相关文章
- HDU4372 Count the Buildings —— 组合数 + 第一类斯特林数
题目链接:https://vjudge.net/problem/HDU-4372 Count the Buildings Time Limit: 2000/1000 MS (Java/Others) ...
- HDU4372 Count the Buildings (+题解:斯特林数)
题面 (笔者翻译) There are N buildings standing in a straight line in the City, numbered from 1 to N. The h ...
- 【HDU 4372】 Count the Buildings (第一类斯特林数)
Count the Buildings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 4372 Count the Buildings
Count the Buildings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- Count the Buildings
K - Count the Buildings 参考:Count the Buildings 思路可以借鉴,但是代码略有问题 写的时候 re 了 9 发,然后把变量定义的顺序换了一下居然 A 了,以为 ...
- 【HDU4372】Count the Buildings (第一类斯特林数)
Description $N$座高楼,高度均不同且为$1~N$中的数,从前向后看能看到$F$个,从后向前看能看到$B$个,问有多少种可能的排列数. $T$组询问,答案模$1000000007$.其中$ ...
- HDU 4372 Count the Buildings:第一类Stirling数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4372 题意: 有n栋高楼横着排成一排,各自的高度为1到n的一个排列. 从左边看可以看到f栋楼,从右边看 ...
- HDU 4372 Count the Buildings [第一类斯特林数]
有n(<=2000)栋楼排成一排,高度恰好是1至n且两两不同.现在从左侧看能看到f栋,从右边看能看到b栋,问有多少种可能方案. T组数据, (T<=100000) 自己只想出了用DP搞 发 ...
- HDU 4372 Count the Buildings——第一类斯特林数
题目大意:n幢楼,从左边能看见f幢楼,右边能看见b幢楼 楼高是1~n的排列. 问楼的可能情况 把握看到楼的本质! 最高的一定能看见! 计数问题要向组合数学或者dp靠拢.但是这个题询问又很多,难以dp ...
随机推荐
- Go语言的各种Print函数
Go语言的各种Print函数 func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) func Pr ...
- [转]坐在马桶上看算法:只有五行的Floyd最短路算法
此算法由Robert W. Floyd(罗伯特·弗洛伊德)于1962年发表在“Communications of the ACM”上.同年Stephen Warshall(史蒂芬·沃舍尔)也独立发表了 ...
- Python raw_input和input总结 在版本2和版本3中的区别
Python 2.3.4 (#1, Feb 2 2005, 11:44:13) [GCC 3.4.3 20041212 (Red Hat 3.4.3-9.EL4)] on linux2 Type &q ...
- JS日期与字符串相互转换!!
一 日期转字符串 dateToString: function(date){ var year = date.getFullYear(); var month =(date.getMonth() + ...
- 【比赛游记】THUWC2019酱油记
往期回顾:THUSC2018酱油记 day 0 早上 7 点的动车,不知道是从哪儿到哪儿的(雾),只知道从福建到广东 233333 一个值得思考的问题:福建人会不会被广东人吃啊? 动车上玩空洞骑士,可 ...
- 项目中遇到的问题:Gradle传递性依赖冲突
问题描述: 在调用别人接口时,由于他们接口做了拦截处理在使用RestTemplate调用时必须要使用@Qualifier("他们封装好的类"),需要导入jar包 gradle方式导 ...
- Ubuntu下软件安装方式、PATH配置、查找安装位置
Ubuntu 18.04, 安装方式 目前孤知道的Ubuntu下安装软件方式有3种(命令): 1.make 2.apt/apt-get 3.dpkg 方式1基于软件源码安装,需要经历配置(可选).编译 ...
- Windows 8 应用程序前后台切换事件监听
在一些情况下,我们需要监听应用程序切换到后台或者从后台切换至前台的事件,从而进行相关处理操作.支付宝应用锁屏(IOS,Android平台)的处理中就需要监听此事件,在用户将应用切换至后台一段时间后再切 ...
- 【Android开发日记】之入门篇(九)——Android四大组件之ContentProvider
数据源组件ContentProvider与其他组件不同,数据源组件并不包括特定的功能逻辑.它只是负责为应用提供数据访问的接口.Android内置的许多数据都是使用ContentProvider形式,供 ...
- Python *args **kw
当函数的参数不确定时,可以使用*args 和**kwargs,*args 没有key值,**kwargs有key值. *args def fun_var_args(farg, *args): prin ...