Test instructions: There are n beef fort and N Chicken Fort for 2n guests to eat, before eating toss a coin to decide what to eat, if the rest of the hamburger, do not have to vote, ask the last two people to eat the same probability.
Analysis: Because the positive consideration also want to do not throw coins, too troublesome, so we first ask the last two people to eat different probability can, then use 1 minus on OK.
Suppose the last two people eat differently, then the former n-2 personally eat is N/2-1 beef fort and n/2-1 Chicken Fort, according to the permutation combination, the probability should be C (n-2, n/2-1) * (0.5) ^ (n-2).
This is the formula, but this is not very good, it is likely to time out, so we will write the first n-2, contrast, and then get a recursive formula:
Dp[i] = dp[i-1] * (2*i-3.0) * (2*i-2.0)/(i-1.0)/(i-1.0) * 0.25; (Note that I is equal to the original 2*i), then you can just forget it.
The code is as follows:
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < cmath> #include <stack> #define FREOPENR freopen ("In.txt", "R", stdin) #define FREOPENW freopen ("OUT.txt", "W", STDOUT) using namespace Std;typedef long Long ll;typedef pair<int, int> p;const int inf = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f;const LL LNF = 0x3f3f3f3f3f3f;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const int MAXN = 5000 0 + 5;const int mod = 1e9 + 7;const int dr[] = {-1, 0, 1, 0};const int dc[] = {0, 1, 0, -1};const char *hex[] = {"0000", " 0001 "," 0010 "," 0011 "," 0100 "," 0101 "," 0110 "," 0111 "," "," 1001 "," 1010 "," 1011 "," 1100 "," 1101 "," 1110 "," 1111 "};int N, m;const int mon[] = {0, 31, 28, 31, 30, 31, 30, [0], +, +, +, 31};const int monn[] = (= {+), H, H, H, +, +,,,,, int, int. b) {return a < b a:b;} inline int Max (int a, int b) {return a > b a:b;} inline ll Min (ll A, ll b) {return a < b a:b;} inline ll Max (ll A, ll b) {return a > b a:b;} inline bool Is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && C < m;} Double dp[maxn];void init () {dp[1] = 1.0; for (int i = 2; I <= 50000; ++i) dp[i] = dp[i-1] * (2*i-3.0) * (2*i-2.0)/(i-1.0)/(i-1.0) * 0.25;} int main () {init (); int T; Cin >> T; while (t--) {cin >> n; printf ("%.4f\n", 1.0-DP[N/2]); } return 0;}
UVa 557 Burger (probability + recursion)