[hdu4372]Count the Buildings【stirling数】

【题目链接】
  http://acm.hdu.edu.cn/showproblem.php?pid=4372
【题解】
  首先最高的一定能看到。
  那么我们可以把序列划分为左边和右边,一共 n1 n − 1 个数,左边能看到 x1 x − 1 ,右边能看到 y1 y − 1 个数。接下来,可以把左边分为 x1 x − 1 段,每一段的第一个为可见的,右边同理。同时 n1 n − 1 个数的排列等于 n n 个数的环排列。因此答案等价于n1个数分配给 x+y2 x + y − 2 个环排列的方案数再乘以组合数 C(x+y2,x1) C ( x + y − 2 , x − 1 ) ,就是第一类斯特林数的 S(n1,x+y2)C(x+y2,x1) S ( n − 1 , x + y − 2 ) ∗ C ( x + y − 2 , x − 1 )
  时间复杂度 O(N2+T) O ( N 2 + T )
【代码】

/* - - - - - - - - - - - - - - -
    User :      VanishD
    problem :   [hdu4372] 
    Points :    stirling
- - - - - - - - - - - - - - - */
# include <bits/stdc++.h>
# define    ll      long long
# define    inf     0x3f3f3f3f
# define    N       2010
# define    M       2000
using namespace std;
int read(){
    int tmp = 0, fh = 1; char ch = getchar();
    while (ch < '0' || ch > '9'){ if (ch == '-') fh = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9'){ tmp = tmp * 10 + ch - '0'; ch = getchar(); }
    return tmp * fh;
}
const int P = 1e9 + 7;
int s[N][N], c[N][N];
int main(){
//  freopen(".in", "r", stdin);
//  freopen(".out", "w", stdout);
    s[0][0] = 1;
    for (int i = 1; i <= M; i++)
        for (int j = 1; j <= i; j++)
            s[i][j] = (s[i - 1][j - 1] + 1ll * (i - 1) * s[i - 1][j]) % P;
    for (int i = 0; i <= M; i++){
        c[i][0] = 1;
        for (int j = 1; j <= i; j++)
            c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % P;
    }
    for (int opt = read(); opt > 0; opt--){
        int n = read(), x = read(), y = read();
        if (x + y - 1 > n)  printf("%d\n", 0);
            else printf("%lld\n", 1ll * s[n - 1][x - 1 + y - 1] * c[x - 1 + y - 1][x - 1] % P);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值