错排递推公式:
d(n) = (n-1)*(d[n-1]+d[n-2]);
证明:将第n个元素放到第k处,第k处的元素如果放到第n处,就是d(n-2),否则,先假设放到第n处,然后错排,就是d(n-1);
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 double fac[22] = {1,1}; 6 double d[22] = {0,0,1,2}; 7 8 int main() 9 { 10 for(int i=1;i<21;i++) 11 fac[i] = i*fac[i-1]; 12 13 for(int i=4;i<21;i++) 14 d[i] = (i-1)*(d[i-1]+d[i-2]); 15 16 int c; 17 scanf("%d",&c); 18 while(c--) { 19 int n; 20 scanf("%d",&n); 21 printf("%.2f%c ",d[n]*100/fac[n],'%'); 22 } 23 24 return 0; 25 }