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 ...
随机推荐
- iOS ShareSDK 三方分享/登录使用
原文 http://www.cnblogs.com/CoderAlex/p/4860352.html 一: 快速集成 1.前言 作为现在App里必不可少的用户分享需要,社交化分享显然是我们开发app里 ...
- 正则表达式 cheat sheet
- php项目报错 Warning: session_start(): open(D:/software/wamp/wamp/tmp\sess_msrjot7f32ciqb1p2hr4ahejg4, O_RDWR) f
今天一个php项目报错: Warning: session_start(): open(D:/software/wamp/wamp/tmp\sess_msrjot7f32ciqb1p2hr4ahejg ...
- bzoj 1835: [ZJOI2010]base 基站选址
Description 有N个村庄坐落在一条直线上,第i(i>1)个村庄距离第1个村庄的距离为Di.需要在这些村庄中建立不超过K个通讯基站,在第i个村庄建立基站的费用为Ci.如果在距离第i个村庄 ...
- SharedPreferences 存List集合,模拟数据库,随时存取
PS:SharedPreferences只要稍微学过一点就会用,他本身通过创建一个Editor对象,来存储提交,而editor可以存的格式为 他里面可以存一个Set<String> Set ...
- lesson - 8 课程笔记 tar / gzip /bzip2 / xz /
作用:为linux的文件和目录创建档案,也可以在档案中改变文件,或者向档案中加入新的文件即用来压缩和解压文件.tar本身不具有压缩功能.他是调用压缩功能实现的 语法:tar[必要参数][选择参数][ ...
- MySql Schema 优化
MySQL Schema 优化: 1.保证你的数据库的整洁性. 2.归档老数据 — 删除查询中检索或返回的多余的行 3.在数据上加上索引. 4.不要过度使用索引,评估你的查询. 5 ...
- python爬虫爬取大众点评并导入redis
直接上代码,导入redis的中文编码没有解决,日后解决了会第一时间上代码!新手上路,多多包涵! # -*- coding: utf-8 -*- import re import requests fr ...
- JSP的三种注释方式
HTML注释(输出注释):指在客户端查看源代码时能看见注释.例如, <!-- this is an html comment.it will show up int the response. ...
- gcc编译器用法
一个用c语言写的程序把他编译成计算机可执行的文件,一般有4个步骤 /*================================================================ ...