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的环的排列数等于n-1个元素的排列数。

综上得出答案,ans=c[f+b-2][f-1]*s[n-1][f+b-2]

#include<cstdio>  
#include<cstring>  
#include<cmath>  
#include<cstdlib>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<queue>  
#include<stack> 
#include<string>
#include<map> 
#include<set>
#define eps 1e-6 
#define LL long long  
#define pii (pair<int, int>)
//#pragma comment(linker, "/STACK:1024000000,1024000000") 
using namespace std;  

const int maxn = 2000 + 200;
//const int INF = 0x3f3f3f3f;
//freopen("input.txt", "r", stdin);
//LL d[maxn][maxn];
LL c[maxn][maxn], s[maxn][maxn];
void init() {
	for(int i = 1; i <= 2005; i++) {
		s[i][0] = 0; s[i][i] = 1;
		for(int j = 1; j < i; j++) {
			s[i][j] = ((i-1)*s[i-1][j]+s[i-1][j-1]) % 1000000007;  
		}
	}
} 
void init2() {
	c[0][0] = 1;
	for(int i = 1; i <= 2005; i++) {
		c[i][0] = 1;
		for(int j = 1; j <= i; j++) {
			c[i][j] = c[i-1][j-1]+c[i-1][j];
			if(c[i][j] >= 1000000007) c[i][j] -= 1000000007;
		}
	}
}
int main() {
	init();
	init2();
	int T; cin >> T;
	while(T--) {
		int n, f, b; scanf("%d%d%d", &n, &f, &b);
		LL ans = (f+b-2 <= n && f+b-2 >= 1)? c[f+b-2][f-1]*s[n-1][f+b-2]% 1000000007 : 0;
		printf("%I64d\n", ans);
	}
	return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值