思路:错排和组合数问题dp[i]表示i个位置错排个数,m个位置错排dp[m], 从n个人中选m个人为C(n, m), 则所求dp[m] * C(n, m)
#include <string.h>
#include <stdio.h>
long long combination(int n, int m){//求组合数
if(n == m || m == 0) return 1;
else return combination(n - 1, m) + combination(n - 1, m - 1);
}
int main(int argc, char** argv) {
int num, n, m;
long long dp[25];
dp[1] = 0;
dp[2] = 1;
for(int i = 3; i <= 20; i++){
dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2]);
}
scanf("%d", &num);
for(int i = 0; i < num; i++){
scanf("%d%d", &n, &m);
printf("%lld\n", dp[m] * combination(n, m));
}
return 0;
}