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 ...
随机推荐
- Git 工作流程
Git 作为一个源码管理系统,不可避免涉及到多人协作. 协作必须有一个规范的工作流程,让大家有效地合作,使得项目井井有条地发展下去.”工作流程”在英语里,叫做”workflow”或者”flow”,原意 ...
- RedHat 4下无resize2fs命令
在Red Hat Enterprise Linux AS release 4上进行LVM扩展分区时,发现RedHat 4下没有resize2fs,不过可以用ext2online替换resize2fs. ...
- DPA/Ignite由于DNS问题导致连接不上被监控的数据库服务器
问题描述: 在DPA(Ignite)的管理监控界面发现有两台SQL Server数据库服务器连接不上,截图如下所示,检查其日志内容 具体错误日志如下所示, Notice:日志里面具体的服务器名称被我用 ...
- Linux iptables 防火墙
内容摘要 防火墙 防火墙定义 防火墙分类 netfilter/iptables netfilter 设计架构 iptables 简述 iptables 命令详解 命令语法 table 参数 comma ...
- PHP无限级分类的实现(不使用递归)
无限级分类在开发中经常使用,例如:部门结构.文章分类.无限级分类的难点在于“输出”和“查询”,例如 将文章分类输出为<ul>列表形式: 查找分类A下面所有分类包含的文章. 1.实现原理 在 ...
- Oracle转移数据表空间存储位置
问题描述:Oracle表空间创建到了C盘,发现C盘的空间不够,现在将表空间的文件转移到D盘下. 操作方法: 1. 先登录sqlplus,登录用户.在cmd中输入:sqlplus /nologSQL&g ...
- CDH离线数据导入solr:利用MapReduceIndexerTool将json文件批量导入到solr
场景描述:前段时间,将实时数据通过kafka+flume+morphline的方式接入到solr中.新进来的数据已经可以在solr中看到了,但是以前的历史数据还没有导入solr. CDH提供利用Map ...
- 烂泥:haproxy学习之手机规则匹配
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 今天我们来介绍下有关haproxy匹配手机的一些规则配置. 一.业务需要 现在根据业务 ...
- ARM嵌入式开发板
iTOP-4412 ARM嵌入式开发板----主要特点 iTOP-4412开发平台是北京迅为电子研发设计的嵌入式开发板平台,核心板配备64位双通道2GB DDR3,16GBEMMC存储,三星原厂S5M ...
- 数据结构(Java描述)之线性表
基础概念 数据结构:是相互之间存在一种或多种关系的数据元素的集合. 逻辑结构和物理结构 关于数据结构,我们可以从逻辑结构和物理结构这两个维度去描述 逻辑结构是数据对象中数据元素之间的关系,是从逻辑意义 ...