You have been out of Syria for a long time, and you recently decided to come back. You remember that you have M friends there and since you are a generous man/woman you want to buy a gift for each of them, so you went to a gift store that have N gifts, each of them has a price.
You have a lot of money so you don’t have a problem with the sum of gifts’ prices that you’ll buy, but you have K close friends among your M friends you want their gifts to be expensive so the price of each of them is at least D.
Now you are wondering, in how many different ways can you choose the gifts?
Input
The input will start with a single integer T, the number of test cases. Each test case consists of two lines.
the first line will have four integers N, M, K, D (0 ≤ N, M ≤ 200, 0 ≤ K ≤ 50, 0 ≤ D ≤ 500).
The second line will have N positive integer number, the price of each gift.
The gift price is ≤ 500.
Output
Print one line for each test case, the number of different ways to choose the gifts (there will be always one way at least to choose the gifts).
As the number of ways can be too large, print it modulo 1000000007.
Examples
Input
2
5 3 2 100
150 30 100 70 10
10 5 3 50
100 50 150 10 25 40 55 300 5 10
Output
3
126
题意:n个物品里面选m个,其中有k个的价值要大于d,求方法数 |
思路:
本来直接想
C
c
n
t
k
∗
C
n
−
k
m
−
k
C^{k}_{cnt}*C^{m-k}_{n-k}
Ccntk∗Cn−km−k 来算的(cnt代表大于等于d的数量),但这样会有大量重复。所以要分步处理,把cnt个看成一堆A,剩下的看成一堆B,我可以一开始从A中选出一定要有的k个,再从B中选出剩下的m-k个:
C
c
n
t
k
∗
C
n
−
c
n
t
m
−
k
C^{k}_{cnt}*C^{m-k}_{n-cnt}
Ccntk∗Cn−cntm−k
然后再从A里多挑出一个,B里少挑一个:
C
c
n
t
k
+
1
∗
C
n
−
c
n
t
m
−
k
−
1
C^{k+1}_{cnt}*C^{m-k-1}_{n-cnt}
Ccntk+1∗Cn−cntm−k−1
A里多挑出2个,B里少挑2个:
C
c
n
t
k
+
2
∗
C
n
−
c
n
t
m
−
k
−
2
C^{k+2}_{cnt}*C^{m-k-2}_{n-cnt}
Ccntk+2∗Cn−cntm−k−2
…
以此类推,最后累加即可。
AC代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
ll dp[600][600];
ll n;
void init()
{
n = 550;
rep(i,0,n) dp[i][0] = 1;
rep(i,1,n) rep(j,1,i) dp[i][j] = (dp[i-1][j] + dp[i-1][j-1])%1000000007;
}
int main()
{
int kase; init();
cin>>kase;
while(kase--)
{
ll m, k, d;
cin>>n>>m>>k>>d;
ll cnt = 0;
rep(i,1,n)
{
ll x = read();
if(x>=d) cnt++;
}
ll ans = 0;
while(k<=cnt&&m-k>=0)
{
ans = (ans + dp[cnt][k] * dp[n-cnt][m-k])%1000000007;
k++;
}
cout<<ans<<'\n';
}
return 0;
}