【題目鏈接】
http://acm.hdu.edu.cn/showproblem.php?pid=4372
【題解】
首先最高的一定能看到。
那麼我們可以把序列劃分爲左邊和右邊,一共 個數,左邊能看到 ,右邊能看到 個數。接下來,可以把左邊分爲 段,每一段的第一個爲可見的,右邊同理。同時 個數的排列等於 個數的環排列。因此答案等價於 個數分配給 個環排列的方案數再乘以組合數 ,就是第一類斯特林數的 。
時間複雜度
【代碼】
/* - - - - - - - - - - - - - - -
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;
}