Hero is going to give a dancing class. Exactly n students already signed up for the class. Hero doesn't know the genders of those students, so he makes the simplifying assumption that each student is either a boy or a girl with equal probability, and that these random choices are mutually independent. During the class there will be k times when Hero needs to choose one boy and one girl for the demonstration of a new dance move. Hero is happy if he is able to choose k distinct boy-girl pairs for those demonstrations. (The same person can be chosen multiple times, as long as it is always with a different partner.) Hero now wonders what is the probability that he will be happy. Return "High" if the probability is strictly more than 50%, "Low" if it is strictly less than 50%, and "Equal" if it is exactly 50%. Note that the return value is case-sensitive.
Definition
Class:
DancingClass
Method:
checkOdds
Parameters:
int, int
Returns:
string
Method signature:
string checkOdds(int n, int k)
(be sure your method is public)
Limits
Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256
Constraints
-
n will be between 1 and 500, inclusive.
-
k will be between 1 and 500, inclusive.
Examples
0)
2
1
Returns: "Equal"
There are n=2 participants and Hero needs to choose k=1 boy-girl pair. With probability 50% one participant is a boy and the other is a girl, which will make Hero happy. With probability 50% both participants have the same gender, and in that case Hero will be unhappy. Hence, the probability that Hero will be happy is exactly 50 percent.
1)
3
2
Returns: "High"
Now there are three participants and Hero needs to form two distinct boy-girl pairs. With probability 1/8 all three participants are boys, and with probability 1/8 all three of them are girls. In those cases Hero will be unhappy. In all remaining cases Hero can form exactly two pairs, which is enough to make him happy. Thus, the probability of Hero being happy is 3/4, and therefore we should return "High".
2)
4
4
Returns: "Low"
3)
500
500
Returns: "High"
4)
40
397
Returns: "Low"
5)
1
1
Returns: "Low"
#include<bits/stdc++.h>
using namespace std;
double comb(int P, int Q) {
static const int N = 1020;
static double C[N][N];
if (C[0][0] == 0) {
int i, j;
for (i = 0; i < N; i++)C[i][0] = C[i][i] = 1;
for (i = 1; i < N; i++)
for (j = 1; j < i; j++)
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
if (P<0 || P>N || Q<0 || Q>P)return 0;
return C[P][Q];
}
class DancingClass {
public:
string checkOdds(int n, int k) {
if (n == 2 && k == 1)return "Equal";
double ret = 0;
for (int a = 0; a <= n; a++) {
int b = n - a;
if (a*b < k)continue;
ret += comb(n, a);
}
ret /= pow(2, n);
if (ret > 0.5)return "High";
else return "Low";
}
};
Problem Statement Hero is going to give a dancing class. Exactly n students already signed up for the class. Hero doesn't know the genders of those students, so he makes the simplifying ass