#HDU4372#Count the Buildings(第一类Stirling数经典)

Count the Buildings

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1512    Accepted Submission(s): 508

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

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

这题是我参考一篇博文做的,很经典的思考方式,写得也很详细:http://blog.csdn.net/acdreamers/article/details/9732431

建议先看以下,对第一类stirling数的解释,更容易理解。

第一类Stirling:将n个不同的数分成k个非空循环排列一共有多少种方法。

分析:

考虑第n个数,n可以单独构成一个非空循环排列,这样前n-1种数构成k-1个非空循环排列,方法数为s1(n-1,k-1);

也可以前n-1种数构成k个非空循环排列,而第n个数插入第i个数的左边,这有(n-1)*s1(n-1,k)种方法。

S1(n,k)=S1(n-1,k-1)+(n-1)*S1(n-1,k)


Code:

Status Accepted
Time 171ms
Memory 47880kB
Length 663
Lang G++
Submitted
Shared
RemoteRunId 21024287

#include
#include
#include
using namespace std;

typedef long long LL;

const int MOD = 1e9 + 7;

LL C[2005][2005], S1[2005][2005];

void pre_work(){
	C[0][0] = 1;
	for(int i = 1; i <= 2000; ++ i){
		C[i][0] = C[i][i] = 1;
		S1[i][0] = 0, S1[i][i] = 1;
		for(int j = 1; j < i; ++ j)
			C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD,
			S1[i][j] = (S1[i - 1][j - 1] + (i - 1) * S1[i - 1][j]) % MOD;
	}
}

int main(){
	pre_work();
	int T, N, b, f;
	scanf("%d", &T);
	while(T --){
		scanf("%d%d%d", &N, &f, &b);
		if(f + b - 2 > 2000)	puts("0");
		else printf("%I64d\n", C[f + b - 2][f - 1] * S1[N - 1][b + f - 2] % MOD);
	}
	return 0;
}







你可能感兴趣的:(HDU,math,组合数学)