Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)
Description
Hasan and Bahosain want to buy a new video game, they want to share the expenses. Hasan has a set of N coins and Bahosain has a set of M coins. The video game costs W JDs. Find the number of ways in which they can pay exactly W JDs such that the difference between what each of them payed doesn’t exceed K.
In other words, find the number of ways in which Hasan can choose a subset of sum S1 and Bahosain can choose a subset of sum S2such that S1 + S2 = W and |S1 - S2| ≤ K.
Input
The first line of input contains a single integer T, the number of test cases.
The first line of each test case contains four integers N, M, K and W (1 ≤ N, M ≤ 150) (0 ≤ K ≤ W) (1 ≤ W ≤ 15000), the number of coins Hasan has, the number of coins Bahosain has, the maximum difference between what each of them will pay, and the cost of the video game, respectively.
The second line contains N space-separated integers, each integer represents the value of one of Hasan’s coins.
The third line contains M space-separated integers, representing the values of Bahosain’s coins.
The values of the coins are between 1 and 100 (inclusive).
Output
For each test case, print the number of ways modulo 109 + 7 on a single line.
Sample Input
24 3 5 182 3 4 110 5 52 1 20 2010 3050
Sample Output
20
思路
题意:
给出两个集合,问有多少种组合形式使得一个集合的子集的和 S1 与另一个集合的子集的和 S2 满足条件 S1 + S2 = W 且 |S1 - S2| < K。
题解:
动态规划01背包的变形。dp[ i ]表示和为 i 共有多少种子集,那么动态规划方程即为 dp[ i ] = dp[ i ] + dp[ i - a[ x ] ]
//dp[ i ]表示和为 i 共有多少种子集 #include<bits/stdc++.h> using namespace std; typedef __int64 LL; const int maxn = 100005; const int mod = 1e9+7; int dp[2][15500]; int main() { int T; scanf("%d",&T); while (T--) { int n,m,k,w; scanf("%d%d%d%d",&n,&m,&k,&w); memset(dp,0,sizeof(dp)); dp[0][0] = dp[1][0] = 1; for (int i = 1;i <= n;i++) { int x; scanf("%d",&x); for (int j = w;j >= x;j--) { dp[0][j] += dp[0][j-x]; while (dp[0][j] >= mod) dp[0][j] -= mod; } } for (int i = 1;i <= m;i++) { int x; scanf("%d",&x); for (int j = w;j >= x;j--) { dp[1][j] += dp[1][j-x]; while (dp[1][j] >= mod) dp[1][j] -= mod; } } LL ans = 0; for (int i = 0;i <= w;i++) { if (abs(w-i-i) <= k) ans = (ans + (LL)dp[0][w-i]*dp[1][i])%mod; } printf("%I64d\n",ans); } return 0; }
Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)的更多相关文章
- Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)
传送门 Description Using at most 7 matchsticks, you can draw any of the 10 digits as in the following p ...
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
A - Coins Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descript ...
- 2016 ACM Amman Collegiate Programming Contest D Rectangles
Rectangles time limit per test 5 seconds memory limit per test 256 megabytes input standard input ou ...
- 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest
Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...
- ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)
题目链接 https://vjudge.net/contest/240074#overview 只写一下自己做的几个题吧 /* D n^2的暴力dp怎么搞都可以的 这里先预处理 i到j的串时候合法 转 ...
- 2015 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...
- 2017 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Watching TV 模拟.统计一下哪个数字最多即可. #include <bits/stdc++.h> using namespace std; const in ...
- 2017 ACM Amman Collegiate Programming Contest
A - Watching TV /* 题意:求出出现次数最多的数字 */ #include <cstdio> #include <algorithm> #include < ...
- gym100712 ACM Amman Collegiate Programming Contest
非常水的手速赛,大部分题都是没有算法的.巨慢手速,老年思维.2个小时的时候看了下榜,和正常人差了3题(,最后还没写完跑去吃饭了.. A 水 Sort 比大小 /** @Date : 2017-09-0 ...
随机推荐
- windows下安装nginx
说起开发,自己感到非常惭愧,由于公司让我给<绿电侠>项目写整体架构解决方案,才开始接触nginx这个东东,突然觉得它是一把非常好的利器. 本文主要记录在windows下安装nginx,另参 ...
- ORACLE清理、截断监听日志文件(listener.log)
在ORACLE数据库中,如果不对监听日志文件(listener.log)进行截断,那么监听日志文件(listener.log)会变得越来越大,想必不少人听说过关于"LISTENER.LOG日 ...
- 基于php基础语言编写的小程序之计算器
基于php基础语言编写的小程序之计算器 需求:在输入框中输入数字进行加.减.乘.除运算(html+php) 思路: 1首先要创建输入数字和运算符的输入框,数字用input的text属性,运算符用sel ...
- 烂泥:haproxy与nginx、zabbix集成
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 昨天介绍了haproxy的手机匹配规则,今天再来介绍下haproxy与nginx.za ...
- 深入java集合系列文章
搞懂java的相关集合实现原理,对技术上有很大的提高,网上有一系列文章对java中的集合做了深入的分析, 先转载记录下 深入Java集合学习系列 Java 集合系列目录(Category) HashM ...
- ELF Format 笔记(十四)—— 段内容
ilocker:关注 Android 安全(新手) QQ: 2597294287 一个段 (segment) 由一个或多个节 (section) 组成,但这对 android linker 是透明的, ...
- tomcat的简单安装及配置
实验系统:CentOS 6.6_x86_64 实验前提:防火墙和selinux都关闭 实验软件:apache-tomcat-8.0.24 jdk-8u60-linux-x64 jeecms-v6 一. ...
- 【转】根据中国气象局提供的API接口实现天气查询
本文转载自 老三 的 三叶草 中国气象局提供了三个天气查询的API接口: [1]http://www.weather.com.cn/data/sk/101190101.html [2]http://w ...
- 复制粘贴出来的悲剧----spring实现文件下载和HttpStatus.CREATED
今天真是被自己的懒惰和复制粘贴给坑惨了... 网上有这么一个spring下载文件的最佳实践: @RequestMapping("download") public Response ...
- 解决:PADS导入.DXF结构图出现坐标超出范围问题
现象: 原因: PCB尺寸是有限的,PADS中对于坐标大小有所限制,但AUTO CAD中的坐标却是无限制的. 解决方法: 1.在AUTO中使用M命令,将绘制的结构图移动至原点: 2.在AUTO中使用W ...