標籤:
D - D
Time Limit:1000MS
Memory Limit:262144KB
64bit IO Format:%I64d & %I64uSubmit Status Practice Gym 100952D
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
25 3 2 100150 30 100 70 1010 5 3 50100 50 150 10 25 40 55 300 5 10
Output
3126
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
Gym 100952D Time to go back 組合學、楊輝三角預先處理組合數