#include <iostream> using namespace std; long long a[21]; /* n 个 数中 m个错排 转化为:充n个数中选取m个数,共有C(n,m)中,选取的m个数进行全部错排 */ /* 排列 */ long long fn(int a,int b) { long long res = 1; for (int i = a; i <= b; i++) { res *= i; } return res; } long long fC(int n, int m) { long long s1 = fn(m+1,n); long long s2 = fn(1,n-m); return s1 / s2; } int main() { int T,n,m; cin >> T; a[1] = 0; a[2] = 1; for (int i = 3; i <= 20; i++) { a[i] = (i - 1) * (a[i - 1] + a[i - 2]); // 全部错排 } while (T--) { cin >> n >> m; long long res = a[m] * fC(n, m); cout << res << endl; } }
部分错排
http://acm.hdu.edu.cn/showproblem.php?pid=2049