uva 11481 - Arrange the Numbers(计数问题)

题目链接:uva 11481 - Arrange the Numbers

题目大意:给出n,m和k,表示有一个序列,由1~n组成,有序,现在将这个序列重排,问有多少种重排序列满足:前m个中恰好有k个的位置不变(即i=pos[i])。

解题思路:首先c=C(km)为前m个数选中k个位置保持不变,然后枚举后n-m个中有多少个数的位置是不变的,C(inm),这样就有nki个数为乱序排列。解法和uva10497一样。

#include <cstdio>
#include <cstring>

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;
}

你可能感兴趣的:(uva 11481 - Arrange the Numbers(计数问题))