A - Coins

Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

standard input/output

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 ofM 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 S2 such 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

Input
2
4 3 5 18
2 3 4 1
10 5 5
2 1 20 20
10 30
50
Output
2
0 题目链接:http://codeforces.com/gym/101102/problem/A题意:有两个序列分别有n和m个元素,现在要从两个序列中分别选出两个子集,设他们的和分别是S1和S2,现在使得选出来的结果满足以下两个条件
|S1-S2|<=K,S1+S2 = W;求有多少种选法,结果对1e9+7求余;
对于序列1,可以用dp[i][j]来表示前i个元素的子集构成和为 j 的方法数;那么就可以看成01背包来做即可;
dp[0] = 1;
dp[j] = (dp[j]+dp[j-a[i]])%mod;
两个序列处理完之后得到dp1和dp2,然后对应求结果即可;
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int N = ;
const double eps = 1e-;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+; int n, m, k, w;
int a[], b[];
LL dp1[N], dp2[N]; int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(dp1, , sizeof(dp1));
memset(dp2, , sizeof(dp2)); scanf("%d %d %d %d", &n, &m, &k, &w);
for(int i=; i<=n; i++)
scanf("%d", &a[i]);
for(int i=; i<=m; i++)
scanf("%d", &b[i]); dp1[] = dp2[] = ; for(int i=; i<=n; i++)
{
for(int j=; j>=a[i]; j--)
dp1[j] = (dp1[j]+dp1[j-a[i]])%mod;
}
for(int i=; i<=m; i++)
{
for(int j=; j>=b[i]; j--)
dp2[j] = (dp2[j] + dp2[j-b[i]])%mod;
} LL ans = ; for(int i=; i<=w; i++)
{
int j=w-i;
if(max(i, j) - min(i, j) > k) continue;
ans = (ans + dp1[i]*dp2[j]%mod) % mod;
}
printf("%I64d\n", ans);
}
return ;
}

Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)的更多相关文章

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

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

  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. 2015 ACM Amman Collegiate Programming Contest 题解

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

  6. 2017 ACM Amman Collegiate Programming Contest 题解

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

  7. 2017 ACM Amman Collegiate Programming Contest

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

  8. gym100712 ACM Amman Collegiate Programming Contest

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

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

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

随机推荐

  1. splice JavaScript Array 对象

    定义和用法 splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目. 注释:该方法会改变原始数组. 语法 arrayObject.splice(index,howmany,item1, ...

  2. android 全屏视频播放(SurfaceView + MediaPlayer)

    介绍个第三方: JieCaoVideoPlayer 实现Android的全屏视频播放,支持完全自定义UI.手势修改进度和音量.hls.rtsp,设置http头信息,也能在ListView.ViewPa ...

  3. 【BZOJ】1087: [SCOI2005]互不侵犯King(状压dp)

    http://www.lydsy.com:808/JudgeOnline/problem.php?id=1087 状压dp是第一次写啊,我也是才学TAT.状压dp一般都用一个值表示集合作为dp的一个状 ...

  4. Get the image file(s) some informations,Including the Make,Model,Date/Time,etc

    This is a blog about how to get the image file(s) some informations.Including the Make,Model,Date/Ti ...

  5. linux 快捷键

    截图: 打印: 全屏截图 alt+打印:窗口截图 shift+打印:区域截图 ctrl+打印:截图到剪贴板 显示桌面: ctrl+super+d 最大化最小化窗口 ctrl+alt+up/down 转 ...

  6. Unix Shell中单引号、双引号字符、反斜杠、反引号的使用[转]

    在执行shell脚本的时候,shell将会对脚本中的行进行解释,然后执行:对于一些特殊处理的句子,我们可以使用引号或者反斜线来避免shell解释执行之.如下,当在命令行中输入:echo *child. ...

  7. [转载]VC6中的文件后缀

    VC文件扩展名 .APS:存放二进制资源的中间文件,VC把当前资源文件转换成二进制格式,并存放在APS文件中,以加快资源装载速度. .BMP:位图资源文件. .BSC:浏览信息文件,由浏览信息维护工具 ...

  8. MS14-068 privilege escalation PoC: 可以让任何域内用户提升为域管理员

    https://github.com/bidord/pykek ms14-068.py Exploits MS14-680 vulnerability on an un-patched domain ...

  9. Swift UICollectionView 简单使用

    最近要研究下排布的游戏关卡界面的实现,简单做了个UICollectionView的demo. 先看最后的效果: 下面来看实现的方法把,在Storyboard对应的ViewController中增加一个 ...

  10. Jquery - Select 和 Checkbox 、Textarea的操作

    Checkbox //判断是否选中 if ($(this).is(':checked')) { alert("它处于选中状态"); } else { alert("它不处 ...