[hdu4372]Count the Buildings【stirling数】

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

猜你喜欢

转载自blog.csdn.net/d_vanisher/article/details/80613456