題目大意:給出n,m和k,表示有一個序列,由1~n組成,有序,現在將這個序列重排,問有多少種重排序列滿足:前m個中恰好有k個的位置不變(即i=pos[i])。
解題思路:首先c=C(km)為前m個數選中k個位置保持不變,然後枚舉後n-m個中有多少個數的位置是不變的,C(in?m),這樣就有n?k?i個數為亂序排列。解法和uva10497一樣。
#include
#include
typedef long long ll;
const int N = 1005;
const ll MOD = 1000000007;
int n, m, k;
ll dp[N], c[N][N];
void init () {
memset(c, 0, sizeof(c));
for (int i = 0; i < N; i++) {
c[i][0] = c[i][i] = 1;
for (int j = 1; j < i; j++)
c[i][j] = (c[i-1][j-1] + c[i-1][j]) % MOD;
}
dp[0] = 1;
dp[1] = 0;
for (ll i = 2; i < N; i++)
dp[i] = ((dp[i-1] + dp[i-2]) % MOD * (i-1)) % MOD;
}
ll solve () {
ll ans = 0;
int t = n - m;
for (int i = 0; i <= t; i++)
ans = (ans + c[t][i] * dp[n-k-i]) % MOD;
return (ans * c[m][k]) % MOD;
}
int main () {
init();
int cas;
scanf("%d", &cas);
for (int i = 1; i <= cas; i++) {
scanf("%d%d%d", &n, &m, &k);
printf("Case %d: %lld\n", i, solve());
}
return 0;
}