https://www.nowcoder.com/pat/2/problem/279
解题思路: 错排公式:f(n) = (n-1)*(f(n-1) + f(n-2))
n中选出m个人为C(n,m),所以ans = c(n,m) *f(n)
#include<cstdio>
#include<cstring>
#define N 22
typedef long long LL;
LL C(int n, int m){ //求c(n,m)
LL ans = 1;
for(int i=1;i<=m;i++){
ans = ans*(n-m+i)/i;
}
return ans;
}
LL F[N];
int main(){
F[2]=1;
F[3]=2;
for(int i = 4;i <= 20; i++){
F[i] = (i-1)*(F[i-1] + F[i-2]);
}
int m,n;
while(scanf("%d%d",&n,&m) != EOF){
printf("%lld\n",C(n,m)*F[m]);
}
return 0;
}