[hdu4372]Count the Buildings【stirling數】

【題目鏈接】
  http://acm.hdu.edu.cn/showproblem.php?pid=4372
【題解】
  首先最高的一定能看到。
  那麼我們可以把序列劃分爲左邊和右邊,一共n1 個數,左邊能看到x1 ,右邊能看到y1 個數。接下來,可以把左邊分爲x1 段,每一段的第一個爲可見的,右邊同理。同時n1 個數的排列等於n 個數的環排列。因此答案等價於n1 個數分配給x+y2 個環排列的方案數再乘以組合數C(x+y2,x1) ,就是第一類斯特林數的S(n1,x+y2)C(x+y2,x1)
  時間複雜度O(N2+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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章