Codeforces 2016 ACM Amman Collegiate Programming Contest A. Coins(动态规划/01背包变形)

时间:2022-11-03 18:29:46

传送门

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&period; The Little Match Girl&lpar;贪心)

    传送门 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&sol;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&lpar;7&period;22随机组队娱乐赛&rpar;

    题目链接 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 &lt ...

  9. gym100712 ACM Amman Collegiate Programming Contest

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

随机推荐

  1. wx getlocation

    http://www.cnblogs.com/txw1958/p/weixin-web-location.html http://blog.csdn.net/myfmyfmyfmyf/article/ ...

  2. poj 2528

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 56958   Accepted: 16464 ...

  3. 想让安卓 APP 如丝般顺滑?

    随着安卓手机市场占有率的节节攀升,随便在大街上找几个人估计 80% 用的都是安卓手机吧!用安卓手机的人这么多,不知道大家是否曾经感觉到过 APP 卡顿.死机?是否遇到应用程序无响应.闪退?本文就为大家 ...

  4. 你所不知道的五件事情--java&period;util&period;concurrent&lpar;第一部分&rpar;

                                                                这是Ted Neward在IBM developerWorks中5 things ...

  5. Windows 7 EXE图标丢失修复方法

    有过Win7下的一些EXE文件图标莫名奇妙丢失,但功能却正常的情况吗?这是图标缓存的问题,应该是Win7的bug. 在命令提示符下输入下列命令即可恢复. 以下是代码片段: taskkill /im e ...

  6. Leetcode:Repeated DNA Sequences详细题解

    题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...

  7. 【python学习笔记01】python的数据类型

    python的基本数据类型 整型 int 浮点型 float 真值 bool 字符串 str 列表 list       #[1,2,3] 元组 tuple    #(1,2,3) 字典 dict   ...

  8. hadoop笔记之Hive的数据存储&lpar;内部表&rpar;

    Hive的数据存储(内部表) Hive的数据存储(内部表) 基于HDFS 可使用hadoop给我们提供的web管理工具查看数据.打开管理工具localhost:9000–>Utilities下的 ...

  9. 初识JS

    今儿我遇到一特别恐怖的事儿,JS 刚开始的我看到JS感觉是懵逼的,翻开第一页,感觉是棒棒哒,再看第二页,感觉是easy的,看到第三页是恐怖的,当看到的第四页的时候,我感觉今年的清明节是为我准备的 废话 ...

  10. 【MySql】update用法

    update 语句可用来修改表中的数据, 简单来说基本的使用形式为: update 表名称 set 列名称=新值 where 更新条件; 以下是在表 students 中的实例: 将 id 为 5 的 ...