Count the Buildings(第一类Stirling数的运用)

Count the Buildings

为什么说这是第一类Stirling数呢?是应为Stirling数有两类,
第一类就是给出n个元素,要将这n个元素分成k个 环排列 的方案数。
第二类就是给出n个元素,要将这n个元素分成k个非空子集 的方案数。
这里具体讲第一类。
这题的大概意思是,有高度为1-N的建筑物,把它排成一条直线,高的建筑物会挡住后面的建筑物,
有多少种方法使从看能看到F个建筑,从右看能看到B个建筑。
?输入N F B (0<N, F, B<=2000)
?求答案 mod 1000000007(1e9+7).
? 对于这类问题,我们要抓住关键,让大问题简化。
? 无论从左看还是从右看,最高的一个建筑肯定是可见的,在左边还有 F-1 个可见建筑,右边有 B-1 个。
? 每一个可见建筑和后面不可见的建筑可以组成一个集合,也就是说有 F-1+B-1 个集合,每个集合中第一个元素都是集合
     最高的,而且每个集合的最高元素都是从小到大排列。
? 现在的问题就是把 n-1 个元素分成 F-1+B-1 个有序集合的问题,但是第一个元素为最高。
? 这正好就是 非空 循环排列,第一类斯特林数中提到的名词,每一个不同排列的环,都可以形成唯一一个以最大元素为开头的排列,即把该环从最大元素处打开。
? 那么我们可以得到 s(n-1,F-1+B-1)
? 通过递推公式。
? 然后我们要选取 C(F+B-2,F-1) 个组合放左,其余放右,即答案为 C(F+B-2,F-1)*s(n-1,F+B-2)
? 代码的实现
   
   
for(i=0;i<N;i++)  
  {  
       C[i][0]=1;  
       C[i][i]=1;  
        S[i][0]=0;  
        S[i][i]=1;  
        for(j=1;j<i;j++)  
        {  
            C[i][j]=(C[i-1][j]%MOD+C[i-1][j-1]%MOD)%MOD;  
           S[i][j]=((i-1)%MOD*S[i-1][j]%MOD+S[i-1][j-1]%MOD);  
        }  
    }  


Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 82   Accepted Submission(s) : 29
Font: Times New Roman | Verdana | Georgia
Font Size:  

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

#include <iostream>
#include <cstdio>
#define Mod 1000000007
using namespace std;
__int64 s[2005][2005],c[2005][2005];
void table()
{
int i,j;
for(i=0;i<2005;i++)
{
c[i][0]=1;
c[i][i]=1;
s[i][0]=0;
s[i][i]=1;
for(j=1;j<i;j++)
{
c[i][j]=(c[i-1][j]%Mod+c[i-1][j-1]%Mod)%Mod;
s[i][j]=((i-1)%Mod*s[i-1][j]%Mod+s[i-1][j-1]%Mod)%Mod;
}
}
return ;
}
int main(int argc, char *argv[])
{
int t,n,f,b,i,j;
table();
cin>>t;
while(t--)
{
scanf("%d%d%d",&n,&f,&b);
printf("%I64d\n",c[f+b-2][f-1]*s[n-1][f+b-2]%Mod);
}
return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值