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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 04:00:49

HDU 4372 Count the Buildings(组合数+斯特林数)的相关文章

HDU 4372 Count the Buildings(组合数学-斯特林数,组合数学-排列组合)

Count the Buildings 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

HDOJ 题目4372 Count the Buildings(斯特林第一类数)

Count the Buildings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 738    Accepted Submission(s): 246 Problem Description There are N buildings standing in a straight line in the City, numbere

HDU 4372 Count the Buildings:第一类Stirling数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4372 题意: 有n栋高楼横着排成一排,各自的高度为1到n的一个排列. 从左边看可以看到f栋楼,从右边看可以看到b栋楼,并且高的楼会挡住低的楼. 问你这些楼有多少种排列方法. 题解: 由于高的楼会挡住低的楼,所以这些楼首先会被划分成f+b-2个区域(除去中间最高的楼),并且左边有f-1个,右边有b-1个. 对于一个区域(假设在左边),这个区域由若干栋楼组成,并且最高的楼一定在最左边. 那么,由一个区域

hdu 4041 2011北京赛区网络赛F 组合数+斯特林数 ***

插板法基础知识 斯特林数见百科 1 #include<iostream> 2 #include<cmath> 3 #include<cstdio> 4 #include<cstring> 5 #define LL long long 6 #define eps 1e-7 7 #define MOD 1000000007 8 using namespace std; 9 int c[2001][2001]={1},stir2[1005][1005]={1};

[2016北京集训测试赛17]crash的游戏-[组合数+斯特林数+拉格朗日插值]

Description Solution 核心思想是把组合数当成一个奇怪的多项式,然后拉格朗日插值..:哦对了,还要用到第二类斯特林数(就是把若干个球放到若干个盒子)的一个公式: $x^{n}=\sum _{i=0}^{n}C(n,i)*i!*S(i,x)$ 围观大佬博客(qaq公式太难打了) Code #include<iostream> #include<cstdio> #include<cstring> #include<cmath> using na

hdu 3625 Examining the Rooms —— 第一类斯特林数

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3625 学习斯特林数:https://blog.csdn.net/qq_33229466/article/details/75042895 https://www.cnblogs.com/gzy-cjoier/p/8426987.html http://www.cnblogs.com/zhouzhendong/p/Stirling-Number.html 关于这道题: 得到一把钥匙后,可以连续开的门与钥匙

HDU 4045 Machine scheduling (组合数学-斯特林数,组合数学-排列组合)

Machine scheduling Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1000    Accepted Submission(s): 363 Problem Description A Baidu's engineer needs to analyze and process large amount of data o

HDU 4045 Machine scheduling (第二类斯特林数+DP)

A Baidu's engineer needs to analyze and process large amount of data on machines every day. The machines are labeled from 1 to n. On each day, the engineer chooses r machines to process data. He allocates the r machines to no more than m groups ,and

UVa 10007 &amp; hdu 1131 Count the Trees (卡特兰数)

Count the Trees Time Limit:3000MS    Memory Limit:0KB     64bit IO Format:%lld & %llu SubmitStatus Description  Count the Trees  Another common social inability is known as ACM (Abnormally Compulsive Meditation). This psychological disorder is somewh