http://acm.hdu.edu.cn/showproblem.php?pid=2048
组合数学错排序公式
f(n) = (n-1)*(f(n-1)+f(n-2))
#include <iostream> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iomanip> #include <map> #include <vector> #include <string> #include <queue> #include <set> #include <algorithm> using namespace std; double solve(double x) { if(x == 1) { return 0; } if(x == 2) { return 1; } return (x - 1) * (solve(x-1) + solve(x-2)); } int n; int main() { int T_T; scanf("%d", &T_T); while(T_T--) { scanf("%d", &n); double d = solve(n); for(int i = 2; i <= n; i++) { d /= i; } printf("%.2lf%c\n", d * 100, ‘%‘); } }
时间: 2024-10-14 12:59:04