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

题意就是买礼物,礼物店有N个礼物,你要给M个人买,其中K个人的礼物的价格不低于D,因为你足够土豪,不用担心钱的问题,然后就是剩下的M-K个人可以买贵的,也可以买便宜的。看你心情,问,一共有多少种买礼物的方法。

要注意,那K个人的礼物价格一定要>=D,否则你就不买了,嗯,就这样。

这个题就是排列组合(组合数)的问题,如果一个一个算的话肯定是不可以的,杨辉三角是个好东西,可以用来处理组合数。

一开始不懂为什么杨辉三角可以用来处理组合数,然后就学了一下杨辉三角,老祖宗就是厉害%,杨辉三角原来这么厉害,传送门:http://www.cnblogs.com/ZERO-/p/7219934.html#3741238

看懂之后就好说了,然后就是代码,有一个坑点,再加上细节处理的不好,导致我wa了20多遍,还是看着大佬的才知道哪里错了,%大佬(托脸)。

先贴代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+;
typedef long long ll;
const ll mod=;
ll yh[N][N];
int a[N];
bool cmp(int a,int b){
return a>b;
}
void yanghui(){
memset(yh,,sizeof(yh));
for(int i=;i<;i++){
yh[i][]=;
for(int j=;j<=i;j++)
yh[i][j]=(yh[i-][j-]+yh[i-][j]+mod)%mod;
}
}
int main(){
int t,n,m,k,d,num;
ll ans;
scanf("%d",&t);
yanghui();
while(t--){
scanf("%d%d%d%d",&n,&m,&k,&d);
num=;ans=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>=d)num++;
}
sort(a,a+n,cmp);
if(n-num==)ans=yh[n][m]%mod;
else if(num<k||n<m)ans=; //这里就是如果不符合条件就是0。
else{
for(int i=k;i<=min(num,m);i++){//wa在这里了,i<=min(num,m),因为不知道到底是贵的礼物先选完还是m个人都能买到贵的。
ans=(ans+(yh[num][i]%mod)*(yh[n-num][m-i]%mod))%mod;
}
}
printf("%I64d\n",ans);
}
return ;
}

好啦,先这样吧,后面的还没补呢,慢慢补吧,咸鱼加油~

把wa掉的代码改好之后真的会让人感觉世界都美好了。。。

٩(๑❛ᴗ❛๑)۶

Codeforces Gym100952 D. Time to go back-杨辉三角处理组合数 (2015 HIAST Collegiate Programming Contest)的更多相关文章

  1. Codeforces Gym100952 A.Who is the winner? (2015 HIAST Collegiate Programming Contest)

      A. Who is the winner?   time limit per test 1 second memory limit per test 64 megabytes input stan ...

  2. Codeforces Gym100952 B. New Job (2015 HIAST Collegiate Programming Contest)

      B. New Job   time limit per test 1 second memory limit per test 64 megabytes input standard input ...

  3. Codeforces Gym100952 C. Palindrome Again !!-回文字符串 (2015 HIAST Collegiate Programming Contest)

      C. Palindrome Again !!   time limit per test 1 second memory limit per test 64 megabytes input sta ...

  4. 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 i ...

  5. java实现组合数_n!_杨辉三角_组合数递推公式_回文数_汉诺塔问题

    一,使用计算机计算组合数 1,设计思想 (1)使用组合数公式利用n!来计算Cn^k=n!/k!(n-k)!用递推计算阶乘 (2)使用递推的方法用杨辉三角计算Cn+1^k=Cn^k-1+Cn^k 通过数 ...

  6. CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A.水题 100952B.水题 100952C.回文字符串 100952D.杨辉三角处理组合数 其他题目待续。。。

    哈哈哈哈哈哈哈,最近一直在补题,改各种错误的代码,wa了20多遍,改到心态爆炸,改好之后,感觉世界都美好了(叉会腰~)... A. Who is the winner? time limit per ...

  7. POJ 3187 全排列+杨辉三角(组合数)

    思路: next_permutation()加个递推组合数随便搞搞就A了- //By SiriusRen #include <cstdio> #include <algorithm& ...

  8. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  9. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

随机推荐

  1. IOS客户端的个人中心可以查看自己的博客了。

    IOS客户端的个人中心可以查看自己的博客了. 写这篇是为了在客户端显示之用. 下一步实现在客户端发博客.

  2. 《Cracking the Coding Interview》——第2章:链表——题目4

    2014-03-18 02:27 题目:将一个单链表按照一个值X分为两部分,小于X的部分放在大于等于X的部分之前. 解法:按照值和X的大小,分链表为两条链表,然后连起来成一条. 代码: // 2.4 ...

  3. [译]16-spring基于注解的配置元数据

    从spring2.5起spring框架开始支持java注解的配置元数据.所以除了使用xml配置文件来描述bean的装配之外,你还 可以使用基于java注解的配置元数据来完成同样的功能. spring框 ...

  4. android 在自定义的listview(有刷新加载项)列表中,数据过少时不能铺满整个屏幕时,header和footer同时显示问题

    android  在自定义的listview(有刷新加载项)列表中,数据过少时,当刷新时,加载项也会显示,这是很头疼的一个问题,查阅了一些资料,总结了一个比较不错的方法: 原来代码: @Overrid ...

  5. static_cast AND dynamic_cast

    类型转换是一种机制,让程序员能够暂时或永久性改变编译器对对象的解释.注意,这并不意味着程序员改变了对象本身,而只是改变了对对象的解释. 在很多情况下,类型转换是合理的需求,可解决重要的兼容问题.因此, ...

  6. 设计模式之策略模式的Python实现

    1. 策略模式解决的是什么问题 策略模式解决的应用场景是这样的: 在业务场景中,需要用到多个算法,并且每个算法的参数是需要调整的.那么当不同的行为堆砌到同一个类中时,我们很难避免使用条件语句来选择合适 ...

  7. [洛谷P3805]【模板】manacher算法

    题目大意:给你一个字符串,求出它的最长回文字段 题解:$manacher$算法 卡点:$p$数组未开两倍空间 C++ Code: #include <cstdio> #include &l ...

  8. 算法复习———dijkstra求次短路(poj3255)

    题目: Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

  9. MyBatis原理分析

    MyBatis原理分析   参考博客: 深入理解mybatis原理: http://blog.csdn.net/luanlouis/article/details/40422941 一 . JDBC的 ...

  10. 行为型设计模式之模板方法(Template Method)

    结构 意图 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Te m p l a t e M e t h o d 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 适用性 一次性 ...