Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】
2017-07-24 15:41
597 查看
D. Time to go back
time limit per test:1 secondmemory 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=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } inline void write(ll x) { if(x<0) { putchar('-'); x=-x; } if(x>9) write(x/10); putchar(x%10+'0'); } ll vis[210][210]; ll val[210]; const ll mod=1000000007; bool cmp(ll a,ll b) { return a>b; } int main() { for(int i=0;i<210;i++) { vis[i][0]=1; for(int j=1;j<=i;j++) { vis[i][j]=((vis[i-1][j-1]%mod)+(vis[i-1][j]%mod))%mod; } } ll t=read(); while(t--) { ll ans=0,pos=0; ll n=read(); ll m=read(); ll k=read(); ll d=read(); for(ll i=0;i<n;i++) val[i]=read(); sort(val,val+n,cmp); for(ll i=0;i<n;i++) { if(val[i]>=d) ans++; else break; } if(n-ans==0) pos=vis[ans][m]; else if(ans<k||n<m) pos=0; else { for(ll i=k;i<=ans;i++) { if(m-i<0) break; pos=(pos+(vis[ans][i]*vis[n-ans][m-i])%mod)%mod; } } write(pos); printf("\n"); } return 0; }
相关文章推荐
- Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】
- CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A.水题 100952B.水题 100952C.回文字符串 100952D.杨辉三角处理组合数 其他题目待续。。。
- Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】
- Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
- Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】
- Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
- Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】
- Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】
- CodeForces-2015 HIAST Collegiate Programming Contest-Gym-100952A-Who is the winner?
- Gym 100952 D. Time to go back
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)
- The 2015 China Collegiate Programming Contest G. Ancient Go hdu 5546
- 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015) Disastrous Downtime(前缀和)
- Sudoku - The 2015 China Collegiate Programming Contest && UESTC 1222
- Codeforces Gym 2015 ACM Amman Collegiate Programming Contest
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest(二月十日训练赛)
- 2015 ACM Amman Collegiate Programming Contest D.Alternating Strings【Dp】
- Codeforces Gym 2015 ACM Arabella Collegiate Programming Contest