Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】
D. Time to go back
time limit per test:1 second
memory 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代码:
1 #include <bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 inline ll read()
5 {
6 ll x=0,f=1;
7 char ch=getchar();
8 while(ch<'0'||ch>'9')
9 {
10 if(ch=='-')
11 f=-1;
12 ch=getchar();
13 }
14 while(ch>='0'&&ch<='9')
15 {
16 x=x*10+ch-'0';
17 ch=getchar();
18 }
19 return x*f;
20 }
21 inline void write(ll x)
22 {
23 if(x<0)
24 {
25 putchar('-');
26 x=-x;
27 }
28 if(x>9)
29 write(x/10);
30 putchar(x%10+'0');
31 }
32 ll vis[210][210];
33 ll val[210];
34 const ll mod=1000000007;
35 bool cmp(ll a,ll b)
36 {
37 return a>b;
38 }
39 int main()
40 {
41 for(int i=0;i<210;i++)
42 {
43 vis[i][0]=1;
44 for(int j=1;j<=i;j++)
45 {
46 vis[i][j]=((vis[i-1][j-1]%mod)+(vis[i-1][j]%mod))%mod;
47 }
48 }
49 ll t=read();
50 while(t--)
51 {
52 ll ans=0,pos=0;
53 ll n=read();
54 ll m=read();
55 ll k=read();
56 ll d=read();
57 for(ll i=0;i<n;i++)
58 val[i]=read();
59 sort(val,val+n,cmp);
60 for(ll i=0;i<n;i++)
61 {
62 if(val[i]>=d)
63 ans++;
64 else break;
65 }
66 if(n-ans==0)
67 pos=vis[ans][m];
68 else if(ans<k||n<m)
69 pos=0;
70 else
71 {
72 for(ll i=k;i<=ans;i++)
73 {
74 if(m-i<0)
75 break;
76 pos=(pos+(vis[ans][i]*vis[n-ans][m-i])%mod)%mod;
77 }
78 }
79 write(pos);
80 printf("n");
81 }
82 return 0;
83 }
- ASP.NET MVC是如何运行的(4): Action的执行
- ASP.NET MVC是如何运行的[1]: 建立在“伪”MVC框架上的Web应用
- 在Entity Framework中使用存储过程(五):如何通过存储过程维护多对多关系?
- ASP.NET MVC下基于异常处理的完整解决方案
- 不到40行代码构建正则表达式引擎
- 随便写一篇文章
- Java 10新特性解密
- Android组件化框架项目详解
- ScheduledThreadPoolExecutor原理探究
- TensorFlow下载与安装
- Kotlin基础之内联函数
- 不服来战,看Kotlin如何完爆Java
- 前缀、中缀、后缀表达式
- iOS WebDriverAgent 环境搭建
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 从发布订阅模式入手读懂Node.js的EventEmitter源码
- 手写一个Promise/A+,完美通过官方872个测试用例
- 浅析白盒审计中的字符编码及SQL注入
- JS做类型检测到底有几种方法?看完本文就知道了!
- HDwiki时间延迟盲注及利用代码
- JS作用域和变量提升看这一篇就够了
- 不知道怎么提高代码复用性?看看这几种设计模式吧!
- 框架源码中用来提高扩展性的设计模式
- 不知道怎么提高代码质量?来看看这几种设计模式吧!
- 手写React的Fiber架构,深入理解其原理
- emlog后台作者权限SQL注入
- 手写一个Redux,深入理解其原理
- 手写一个React-Redux,玩转React的Context API
- 使用React-Router实现前端路由鉴权
- 手写React-Router源码,深入理解其原理