HDU 2048 神、上帝以及老天爷
错排公式
1 #include <algorithm> 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 #include <cctype> 6 7 using namespace std; 8 9 inline void read(long long &x) 10 { 11 int k = 1; x = 0; 12 char c = getchar(); 13 while (!isdigit(c)) 14 if (c == '-') k = - 1, c = getchar(); 15 else c = getchar(); 16 while (isdigit(c)) 17 x = (x << 1) + (x << 3) + (c ^ 48), 18 c = getchar(); 19 x *= k; 20 } 21 22 long long T, n, dp[55], mul[55]; 23 24 int main() 25 { 26 read(T), mul[0] = 1; 27 for (int i = 1; i <= 30; ++i) 28 mul[i] = mul[i - 1] * i; 29 for (int plk = 1; plk <= T; ++plk) 30 { 31 read(n); 32 memset(dp, 0, sizeof(dp)); 33 dp[0] = 0, dp[1] = 0, dp[2] = 1; 34 for (int i = 3; i <= n; ++i) 35 dp[i] = (i - 1) * (dp[i - 1] + dp[i - 2]); 36 printf("%.2lf%%\n", 100 * (double)dp[n] / (double)mul[n]); 37 } 38 return 0; 39 }