4372 Count the Buildings(2012 Multi-University Training Contest 8)第一类strling数

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

题意是一共有n栋楼,从一边看,你能看到f栋楼,从另一边看,你能看到b栋楼,问你这种情况下一共有几种排列组合
首先我们固定最高的楼,然后将两边分成f-1和b-1份,用每一份的最高层来表示这一份,每份的最高层从两边到中间肯定是递增的,所以这时候的排列数就是每栋楼的相对位置,也就是第一类strling数,所以这种情况一共有str[n-1][b+f-2],也就是将n-1栋楼分成b+f-2个圈,(剩下的那个圈是最高的那栋楼),然后将b-1个圈放在一边,f-1个圈放在另一边,就是C[b+f-2][b-1],两式相乘就是最终结果

#include<bits/stdc++.h>
using namespace std;
using LL =int64_t;
const int mod=1e9+7;
const int maxn=2005;
LL C[maxn][maxn],str[maxn][maxn];

void init() {
    for(int i=1;i<=2000;i++) {
            C[i][0]=C[i][i]=1;
        for(int j=1;j<i;j++) {
            C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
        }
    }
    for(int i=1;i<=2000;i++) {
        str[i][0]=0,str[i][i]=1;
        for(int j=1;j<i;j++)
        str[i][j]=((i-1)*str[i-1][j]+str[i-1][j-1])%mod;
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    init();
    int n,f,b,T;
    cin>>T;
    while(T--) {
        cin>>n>>f>>b;
        if(f+b-2>2000) cout<<0<<endl;
        else
            cout<<(C[b+f-2][b-1]%mod*str[n-1][b+f-2])%mod<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值