传送门

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. StarkSoft题库管理系统(二)--生成word格式试卷

    一.功能介绍    1.自定义试题库管理系统目录.难易程度,题型,知识库等.    2.试题录入.    3.强大的试题编辑功能,并与通常应用编辑工具有共通.    4.灵活的试卷构造功能,用户可自定 ...

  2. Swift实现封装PopMenu菜单,可在屏幕任意位置弹出

    效果图: 说明: 代码现已支持 Swift3 语法 使用介绍: 1.初始化位置 //frame 为整个popview相对整个屏幕的位置 箭头距离右边位置,默认15 //popMenu = SwiftP ...

  3. android 发送短信功能

    private void sendSMS(String num,String smsBody) { String phoneNum = "smsto:" + num; Uri sm ...

  4. awk-笔记

    语法形式: awk [options] 'script' var=value file1 [file...] awk [options] -f scriptfile var=value file [f ...

  5. netty3升netty4一失眼成千古恨

    老项目是netty3的,本来想直接改到netty5,但是netty5居然是只支持jdk1.7,很奇怪jdk1.6和jdk1.8都不行..为了兼容jdk1.6加上netty4本来和netty5就差别不大 ...

  6. 修复Grub引导

    1.运行终端,输入命令: sudo fdisk -l 找到linux所在的盘符,例如/dev/sdaX 2.获取root权限 3.mount mount –bind /proc /mnt/proc m ...

  7. 【原】移动web动画设计的一点心得——css3实现跑步

    今年3月份,由于公司业务需要,我转岗到微信产品部,离开了TID团队,人都是有感情的动物,更何况在一个团队呆了快 3 年,心中十分舍不得,鬼哥说了“天下没有不散的宴席...”,在我的世界里又多了一次离别 ...

  8. 洛谷P1101 单词方阵——S.B.S.

    题目描述 给一nXn的字母方阵,内可能蕴含多个“yizhong”单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着8个方向的任一方向,同一单词摆放时不再改变方向,单词与单词之间[color=red ...

  9. 【读书笔记《Bootstrap 实战》】1.初识Bootstrap

    作为Web前端开发框架,Bootstrap为大多数标准的UI设计常见提供了用户友好.扩浏览器的解决方案. 1.下载Bootstrap 打开官方网址 http://getbootstrap.com/ 进 ...

  10. java Socket编程-基于UDP

    package com.wzy.UDPTest; import java.net.DatagramPacket; import java.net.DatagramSocket; import java ...