Codeforces Gym100952 D. Time to go back-杨辉三角处理组合数 (2015 HIAST Collegiate Programming Contest)

 

 
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+10;
typedef long long ll;
const ll mod=1000000007;
ll yh[N][N];
int a[N];
bool cmp(int a,int b){
    return a>b;
}
void yanghui(){                                 
    memset(yh,0,sizeof(yh));
    for(int i=0;i<250;i++){
        yh[i][0]=1;
        for(int j=1;j<=i;j++)
        yh[i][j]=(yh[i-1][j-1]+yh[i-1][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=0;ans=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            if(a[i]>=d)num++;
        }
        sort(a,a+n,cmp);
            if(n-num==0)ans=yh[n][m]%mod;
            else if(num<k||n<m)ans=0;          //这里就是如果不符合条件就是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 0;
}

 

 

 

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

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

٩(๑❛ᴗ❛๑)۶

posted @ 2017-07-27 17:20  ZERO-  阅读(341)  评论(0编辑  收藏  举报