|
| 1 | +#include <bits/stdc++.h> |
| 2 | +#define endl '\n' |
| 3 | +typedef long long ll; |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +/* |
| 8 | +PROBLEM STATMENT: |
| 9 | +Arul has a binary array∗ a of length n |
| 10 | +
|
| 11 | +. |
| 12 | +
|
| 13 | +He will take all subsequences† |
| 14 | +of length k (k is odd) of this array and find their median.‡ |
| 15 | +
|
| 16 | +What is the sum of all these values? |
| 17 | +
|
| 18 | +As this sum can be very large, output it modulo 109+7 |
| 19 | +. In other words, print the remainder of this sum when divided by 109+7. |
| 20 | +
|
| 21 | +APPROACH: |
| 22 | +to get the ans we just needed the different combinations of sequence where the median would be one; |
| 23 | +our ans was nothing but diff combinations so for median one (k+1)/2th element must be one so that means there must atleast (k+1)/2 ones |
| 24 | +and atmost k ones now then we have to calculte no of ways of selecting ones and zero from given ones; |
| 25 | +
|
| 26 | +now as we would have to compute ncr many times we used precomputation of fac,and inv fac |
| 27 | +to trade off memory for speed. |
| 28 | +
|
| 29 | +TIME COMPLEXITY:O(MAXN); |
| 30 | +SPACE COMPLEXITY:O(MAXN); |
| 31 | +
|
| 32 | +SUBMISSION LINK: |
| 33 | +https://codeforces.com/contest/1999/submission/356201034 |
| 34 | +
|
| 35 | +*/ |
| 36 | + |
| 37 | +const ll h=1e9+7; |
| 38 | +const ll maxn=2*(1e5)+1; |
| 39 | + |
| 40 | +ll fac[maxn],inv[maxn]; |
| 41 | + |
| 42 | +ll binexp(int a,int b){ |
| 43 | + if(b==0)return 1; |
| 44 | + |
| 45 | + ll set=binexp(a,b/2); |
| 46 | + |
| 47 | + set=(set*set)%h; |
| 48 | + if (b&1) |
| 49 | + return (set*a)%h; |
| 50 | + else |
| 51 | + return set; |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +void precomp(){ |
| 57 | + fac[0]=1; |
| 58 | + for(int i=1;i<maxn;i++)fac[i]=(fac[i-1]*i)%h; |
| 59 | + inv[maxn-1]=binexp(fac[maxn-1],h-2); |
| 60 | + for(int i=maxn-2;i>=0;i--)inv[i]=(inv[i+1]*(i+1))%h; |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +long long comb(int s,int t){ |
| 66 | + ll a= fac[s]; |
| 67 | + ll b=inv[s-t]; |
| 68 | + ll c=inv[t]; |
| 69 | + return (((a*b)%h)*c)%h; |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +void solve(){ |
| 76 | + int n,k;cin>>n>>k;int y;int x=0;ll ans=0; |
| 77 | + for(int i=0;i<n;i++){ |
| 78 | + cin>>y; |
| 79 | + if(y==1)x++; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + for(int i=max((k+1)/2,k-n+x);i<=min(x,k);i++) |
| 84 | + ans=(ans +(comb(x,i)*comb(n-x,k-i))%h)%h; |
| 85 | + |
| 86 | + cout<<ans<<endl; |
| 87 | +} |
| 88 | + |
| 89 | +int main(){ |
| 90 | + ios::sync_with_stdio(0); cin.tie(0); |
| 91 | + int t;cin>>t; |
| 92 | + precomp(); |
| 93 | + while(t--) |
| 94 | + solve(); |
| 95 | + |
| 96 | +} |
0 commit comments