Gym 100952D Time to go back 组合学、杨辉三角预处理组合数

D - D
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

standard input/output
Statements

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.

Sample Input

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

Source

UESTC 2016 Summer Training #21

Gym 100952D


My Solution

组合学

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

领 price >= d 的礼物个数为 cnt

则如果用 C[cnt][k] * C[n - k][m - k] 则显然不对, 因为这里面前面选的 price >= d的, 后面给普通好朋友选礼物的时候也会选到, 这样总的买的礼物数来说有大量重复了

所以 应该是分步 分类(price >= d 的与 < d 的分开算, 这样就不会相同的礼物选2次了)

首先 C[cnt][k]        *    C[n - cnt][m - k];

然后 C[cnt][k + 1]  *   C[n - cnt][ m - k - 1]

接着 C[cnt][k + 2]  *   C[n - cnt][ m - k - 2]

                         ......

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


复杂度 O(T * n)


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 2*1e2 + 8;
const LL Hash = 1000000007;

inline LL mod(LL a)
{
    return a - (a/Hash)*Hash;
}

LL C[maxn][maxn], val[maxn];
inline void getC()
{
    memset(C, 0, sizeof C);
    for(int i = 0; i < maxn; i++){
        C[i][0] = 1;
        for(int j = 1; j <= i; j++){
            C[i][j] = mod(C[i-1][j-1] + C[i-1][j]);
        }
    }
}

inline bool cmp(LL a, LL b)
{
    return a > b;
}

int main()
{

    #ifdef LOCAL
    freopen("a.txt", "r", stdin);
    //freopen("b.txt", "w", stdout);
    #endif // LOCAL
    getC();
    int T, n, m, k, d, cnt;
    LL ans;
    scanf("%d", &T);
    while(T--){
        cnt = 0; ans = 0;
        scanf("%d%d%d%d", &n, &m, &k, &d);
        for(int i = 0; i < n; i++){
            scanf("%I64d", &val[i]);
        }
        sort(val, val + n, cmp);
        for(int i = 0; i < n; i++){
            if(val[i] < d) break;
            else cnt++;
        }
        //cout<<cnt<<endl;
        //cout<<C[n - k][m - k]<<endl;
        for(int i = k; i <= cnt; i++){
            if(n - cnt == 0) break;
            if(m - i < 0) break;
            ans = mod(ans +  mod(C[cnt][i] * C[n - cnt][m - i]));

        }
        if(n - cnt == 0) ans = C[cnt][m];
        //ans = mod(C[cnt][k] * C[n - k][m - k]);
        if(cnt < k || n < m) printf("0\n");
        else printf("%I64d\n", ans);
    }
    return 0;
}

  Thank you!

                                                                                                                                               ------from ProLights

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值