hdu 2048 神、上帝以及老天爷 (错排公式)

hdu2048 题目链接

题意:n个人不放回的从一个有n个每个人对应id的卡片的盒子取一张卡片,取的正好和自己的对应就算中奖。求所有人都没有中奖的概率。

思路:错排。。。

复习了一下错排公式。。。d[n] = (n-1)*(d[n-1]+d[n-2])  (d[1]=0,d[2]=1)

然后求概率的时候。。惊讶得发现概率稳定在了36.79%(1/e)附近。。。

这是因为。。。错排还有一个公式:D(n) = n! [(-1)^2/2! + … + (-1)^(n-1)/(n-1)! + (-1)^n/n!].

求概率每次把n!除掉了。。剩下的。。其实就是e的泰勒展开,当x=-1时的值。

因为当n越大时。。这个概率越接近1/e

这道题里。。。在保留百分数的小数点两位的精度的条件下。。当n为7的时候。。答案就已经是36.79保持不变了。。。

/* ***********************************************
Author :111qqz
Created Time :2016年07月27日 星期三 15时53分04秒
File Name :code/hdu/2048.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=22;
 7double d[N];
 8double f[N];
 9int n;
10int main()
11{
12	#ifndef  ONLINE_JUDGE 
13	freopen("code/in.txt","r",stdin);
14  #endif
1	f[0] = 1;
2	for ( int i = 1; i < N ; i++) f[i] = f[i-1]*1.0*i;
3	d[1] = 0;
4	d[2] = 1;
5	for ( int i = 3 ; i < N ; i++) d[i] = 1.0*(i-1)*(d[i-1]+d[i-2]);
1	int T;
2	cin>>T;
3	while (T--)
4	{
5	    scanf("%d",&n);
6	    double ans = d[n]*100.0/f[n];
7	 //   cout<<"n:"<<n<<" d[n]:"<<d[n]<<" "<<f[n]<<endl;
8	    printf("%.2f%%\n",ans);
	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}