传送门

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 NMK 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背包变形)的更多相关文章

  1. 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 ...

  2. Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)

    A - Coins Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Descript ...

  3. 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 ...

  4. 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 ...

  5. ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)

    题目链接 https://vjudge.net/contest/240074#overview 只写一下自己做的几个题吧 /* D n^2的暴力dp怎么搞都可以的 这里先预处理 i到j的串时候合法 转 ...

  6. 2015 ACM Amman Collegiate Programming Contest 题解

    [题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...

  7. 2017 ACM Amman Collegiate Programming Contest 题解

    [题目链接] A - Watching TV 模拟.统计一下哪个数字最多即可. #include <bits/stdc++.h> using namespace std; const in ...

  8. 2017 ACM Amman Collegiate Programming Contest

    A - Watching TV /* 题意:求出出现次数最多的数字 */ #include <cstdio> #include <algorithm> #include < ...

  9. gym100712 ACM Amman Collegiate Programming Contest

    非常水的手速赛,大部分题都是没有算法的.巨慢手速,老年思维.2个小时的时候看了下榜,和正常人差了3题(,最后还没写完跑去吃饭了.. A 水 Sort 比大小 /** @Date : 2017-09-0 ...

随机推荐

  1. SqlServer--代码创建约束

    约束-保证数据完整性先用设计器创建约束,再用代码创建约束.数据库约束是为了保证数据的完整性(正确性)而实现的一套机制非空约束 (选择复选框)not null主键约束(PK)primary key co ...

  2. VIEW SERVER STATE permission was denied on object 'server', database 'master'

    今天一同事反馈使用SQL Server 2012 Management Studio连接SQL Server 2014后,选择数据库中某个表,然后单击右键时,就会遇到下面错误: 这个错误初看以为是权限 ...

  3. linux shell for循环使用命令中读取到的值实例

    #!/bin/bash file="states" for state in `cat $file` do echo "Visit beautiful $state&qu ...

  4. mysql 常用sql

    1.查询数据库.表的情况 show engines; #数据库的存储引擎show create TABLE User_Base_Info;#显示create table的sql语句show table ...

  5. 如何使用 OpenStack CLI - 每天5分钟玩转 OpenStack(22)

    本节首先讨论 image 删除操作,然后介绍 OpenStack CLI 的使用方法,最后讨如何 Troubleshoot. Web UI 删除 image admin 登录后,Project -&g ...

  6. java自带工具-javap使用

    javap是JDK自带的反汇编器,可以查看java编译器为我们生成的字节码.通过它,我们可以对照源代码和字节码,从而了解很多编译器内部的工作,有助与我们更加理解java特性. javap(反汇编命令) ...

  7. cefsharp设置网页接受语言Accept-Language

    1.设置浏览器的请求控制器 webView.RequestHandler = new RequestHandler(); 2.新建RequestHandler类继承IRequestHandler接口, ...

  8. js面向对象与原型

    创建对象 var box = new Object();//创建对象 box.name = 'Lee'; //添加属性 box.age = 100; box.run = function(){ ret ...

  9. Struts2 自定义MVC框架

    一.Model1与Model2: Model1:就是一种纯jsp开发技术,将业务逻辑代码和视图渲染代码杂糅在一起. Model2:Model2是在Model1的基础上,将业务逻辑的代码分离开来,单独形 ...

  10. [No00007E]2016-面经[中]

    目录: 写一份动人简历的九个步奏 英文简历必备的十大元素 写一份动人简历的九个步骤 写一份动人的简历可以算得上是找工作最难的部分之一,但是,通过下面九步,这件事不再那么难了. 简历定位.雇主们之所以花 ...