hdu 2048 神、上帝以及老天爷 (错排公式)
题意: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保持不变了。。。
1/* ***********************************************
2Author :111qqz
3Created Time :2016年07月27日 星期三 15时53分04秒
4File Name :code/hdu/2048.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=22;
34double d[N];
35double f[N];
36int n;
37int main()
38{
39 #ifndef ONLINE_JUDGE
40 freopen("code/in.txt","r",stdin);
41 #endif
42
43 f[0] = 1;
44 for ( int i = 1; i < N ; i++) f[i] = f[i-1]*1.0*i;
45 d[1] = 0;
46 d[2] = 1;
47 for ( int i = 3 ; i < N ; i++) d[i] = 1.0*(i-1)*(d[i-1]+d[i-2]);
48
49 int T;
50 cin>>T;
51 while (T--)
52 {
53 scanf("%d",&n);
54 double ans = d[n]*100.0/f[n];
55 // cout<<"n:"<<n<<" d[n]:"<<d[n]<<" "<<f[n]<<endl;
56 printf("%.2f%%\n",ans);
57
58 }
59
60 #ifndef ONLINE_JUDGE
61 fclose(stdin);
62 #endif
63 return 0;
64}