[HDOJ2048]神、上帝以及老天爷

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, '%');
    }
}

 

posted @ 2015-09-29 18:28  Kirai  阅读(251)  评论(0编辑  收藏  举报