1+ /*
2+ DAY 8
3+ Q2 F. Expected Median
4+
5+ */
6+ /*
7+
8+
9+ For an odd-length subsequence, its median is **1** iff it has at least ((k+1)/2) ones (because the array is binary).
10+ So the answer equals the number of subsequences of length (k) that contain at least ((k+1)/2) ones.
11+ Count subsequences with exactly (t) ones using (\binom{c1}{t}\binom{c0}{k-t}) and sum over all (t \ge (k+1)/2), modulo (10^9+7).
12+
13+
14+ */
15+ #include < bits/stdc++.h>
16+ using namespace std ;
17+
18+ const long long MOD = 1'000'000'007 ;
19+ const int MAXN = 200000 * 2 + 5 ;
20+
21+ long long fact[MAXN], invfact[MAXN];
22+
23+ long long power (long long a, long long b) {
24+ long long r = 1 ;
25+ while (b) {
26+ if (b & 1 ) r = r * a % MOD;
27+ a = a * a % MOD;
28+ b >>= 1 ;
29+ }
30+ return r;
31+ }
32+
33+ long long nCr (int n, int r) {
34+ if (r < 0 || r > n) return 0 ;
35+ return fact[n] * invfact[r] % MOD * invfact[n - r] % MOD;
36+ }
37+
38+ int main () {
39+
40+
41+
42+ fact[0 ] = 1 ;
43+ for (int i = 1 ; i < MAXN; i++) fact[i] = fact[i - 1 ] * i % MOD;
44+ invfact[0 ] = 1 ;
45+ invfact[MAXN - 1 ] = power (fact[MAXN - 1 ], MOD - 2 );
46+ for (int i = MAXN - 2 ; i >= 1 ; i--)
47+ invfact[i] = invfact[i + 1 ] * (i + 1 ) % MOD;
48+
49+ int t;
50+ cin >> t;
51+ while (t--) {
52+ int n, k;
53+ cin >> n >> k;
54+
55+ int c1 = 0 , c0 = 0 ;
56+ for (int i = 0 ; i < n; i++) {
57+ int x; cin >> x;
58+ if (x) c1++;
59+ else c0++;
60+ }
61+
62+ int m = (k + 1 ) / 2 ;
63+ long long ans = 0 ;
64+
65+ for (int t = m; t <= k && t <= c1; t++) {
66+ ans = (ans + nCr (c1, t) * nCr (c0, k - t)) % MOD;
67+ }
68+
69+ cout << ans % MOD << " \n " ;
70+ }
71+
72+ return 0 ;
73+ }
74+
75+ // Tc =O(n+k) per test SC = O(n)
76+ /*
77+ My submission : https://codeforces.com/contest/1999/submission/356297537
78+ */
0 commit comments