Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】
D. Time to go back
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?
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.
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.
2
5 3 2 100
150 30 100 70 10
10 5 3 50
100 50 150 10 25 40 55 300 5 10
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=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(ll x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
}
ll vis[][];
ll val[];
const ll mod=;
bool cmp(ll a,ll b)
{
return a>b;
}
int main()
{
for(int i=;i<;i++)
{
vis[i][]=;
for(int j=;j<=i;j++)
{
vis[i][j]=((vis[i-][j-]%mod)+(vis[i-][j]%mod))%mod;
}
}
ll t=read();
while(t--)
{
ll ans=,pos=;
ll n=read();
ll m=read();
ll k=read();
ll d=read();
for(ll i=;i<n;i++)
val[i]=read();
sort(val,val+n,cmp);
for(ll i=;i<n;i++)
{
if(val[i]>=d)
ans++;
else break;
}
if(n-ans==)
pos=vis[ans][m];
else if(ans<k||n<m)
pos=;
else
{
for(ll i=k;i<=ans;i++)
{
if(m-i<)
break;
pos=(pos+(vis[ans][i]*vis[n-ans][m-i])%mod)%mod;
}
}
write(pos);
printf("\n");
}
return ;
}
Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】的更多相关文章
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
E. Arrange Teams time limit per test:2 seconds memory limit per test:64 megabytes input:standard inp ...
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...
- Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】
J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...
- Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】
I. Mancala time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ou ...
- Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】
H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...
- Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】
G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...
- Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】
C. Palindrome Again !! time limit per test:1 second memory limit per test:64 megabytes input:standar ...
- Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】
B. New Job time limit per test:1 second memory limit per test:64 megabytes input:standard input outp ...
- Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】
A. Who is the winner? time limit per test:1 second memory limit per test:64 megabytes input:standard ...
随机推荐
- [array] leetcode - 54. Spiral Matrix - Medium
leetcode-54. Spiral Matrix - Medium descrition GGiven a matrix of m x n elements (m rows, n columns) ...
- 栈和队列数据结构的相互实现[LeetCode]
栈是先进后出,队列是先进后出,这里讨论一下两种数据结构之间的相互实现. 一.用两个栈实现队列 我们用一个栈来实现队列的进队操作(栈A),用另一个栈来实现队列的出队操作(栈B). 1.入队列: 把元素放 ...
- Java I/O---File类
1.File概述 File(文件)类可能有一定的误导性,它不仅仅指代的是文件.它既能代表一个特定的文件的名称,又能代表一个目录下的一组文件集的名称,这样就可以用集合List方法调用和遍历.实际上Fil ...
- Mysql 锁基础
本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/53 lock与latch 在数据库中,lock与latch都可以 ...
- w 命令详解
作用: 用于显示已经登录系统的用户列表, 并显示用户正在执行的指令. 执行这个命令可得知目前登入系统的用户有哪些人, 以及他们正在执行的程序. 单独执行w 命令会显示所有的用户, 您也可指定用户名称 ...
- MySQL 最左前缀(Leftmost Prefix) & 组合索引(复合索引,多列索引)
资料来源于网络,仅供参考学习. CREATE TABLE test(a INT,b INT,c INT,KEY idx(a,b,c)); 优: SELECT * FROM test WHERE a=1 ...
- Web API系列之一 Rest简介
1.REST:Representational State Transfer表征状态转移,是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格.REST设计风格有如下几点: ...
- linux部署solr服务--小记
1.将solr压缩包上传到web项目-solr文件夹下 2.解压solr-5.5.4.zip到当前文件夹下 linux 解压zip文件到当前目录 unzip filename.zip 提示没有unzi ...
- libcurl的使用
http://blog.csdn.net/ixiaochouyu/article/details/47998267
- tornado返回指定的http code
最近做web api,需要在接口返回异常时,返回对应的http code. 查了下tornado的文档,是通过set_status方法来设置返回的http code,做个记录. self.set_st ...