Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】

D. Time to go back

time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

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

题目链接:http://codeforces.com/gym/100952/problem/D

题意:有 n 个礼物, m个朋友, 其中k 个很要好的朋友, 要买 price 大于 d 的礼物

分析:领 price >= d 的礼物个数为ans,分步分类(price >= d 的与 < d 的分开算,这样就不会相同的礼物选2次了)

首先 vis[ans][k]*vis[n - ans][m - k];

然后vis[ans][k+1]*vis[n-ans][m-k-1];

接着 vis[ans][k+2]*vis[n-ans][m-k-2];

 ......

直到 k + 1 == ans 或者 m - k - 1 < 0    //其中 如果k + 1 == ans  则ans 选完了,而 m - k - 1 < 0 则是 则是全都选了price >= d的了

复杂度 O(T * n)

下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(ll x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
}
ll vis[][];
ll val[];
const ll mod=;
bool cmp(ll a,ll b)
{
return a>b;
}
int main()
{
for(int i=;i<;i++)
{
vis[i][]=;
for(int j=;j<=i;j++)
{
vis[i][j]=((vis[i-][j-]%mod)+(vis[i-][j]%mod))%mod;
}
}
ll t=read();
while(t--)
{
ll ans=,pos=;
ll n=read();
ll m=read();
ll k=read();
ll d=read();
for(ll i=;i<n;i++)
val[i]=read();
sort(val,val+n,cmp);
for(ll i=;i<n;i++)
{
if(val[i]>=d)
ans++;
else break;
}
if(n-ans==)
pos=vis[ans][m];
else if(ans<k||n<m)
pos=;
else
{
for(ll i=k;i<=ans;i++)
{
if(m-i<)
break;
pos=(pos+(vis[ans][i]*vis[n-ans][m-i])%mod)%mod;
}
}
write(pos);
printf("\n");
}
return ;
}
上一篇:【EF学习笔记10】----------主从表级联操作


下一篇:RabbitMQ、Memcache、Redis(队列、缓存)