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

Example

Input
2
4 3 5 18
2 3 4 1
10 5 5
2 1 20 20
10 30
50
Output
2
0

对A和B的硬币分别dp,f[i]表示拼成i元的方案数,for i=1 to n for j=15000 down to 0 f(j+a(i))+=f(j)

最后枚举一下差在K以内,且i+j=W的f(i)和g(j)即可。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
#define MOD 1000000007ll
int T,n,m,K,W;
int a[160],b[160];
int Abs(int x)
{
return x<0 ? (-x) : x;
}
ll f[15010],g[15010];
int main()
{
scanf("%d",&T);
for(;T;--T)
{
scanf("%d%d%d%d",&n,&m,&K,&W);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
for(int i=1;i<=m;++i)
scanf("%d",&b[i]);
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
f[0]=1;
for(int i=1;i<=n;++i)
for(int j=15000;j>=0;--j)
if(a[i]+j<=15000)
f[j+a[i]]=(f[j+a[i]]+f[j])%MOD;
g[0]=1;
for(int i=1;i<=m;++i)
for(int j=15000;j>=0;--j)
if(b[i]+j<=15000)
g[j+b[i]]=(g[j+b[i]]+g[j])%MOD;
ll ans=0;
for(int i=0;i<=W;++i)
if(Abs(W-i-i)<=K)
ans=(ans+f[i]*g[W-i]%MOD)%MOD;
cout<<ans<<endl;
}
return 0;
}

【动态规划】Gym - 101102A - Coins的更多相关文章

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

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

  2. Codeforces Gym - 101102A - Coins

    A. Coins 题目链接:http://codeforces.com/gym/101102/problem/A time limit per test 3 seconds memory limit ...

  3. codeforce Gym 101102A Coins (01背包变形)

    01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...

  4. 动态规划:HDU-1398-Square Coins(母函数模板)

    解题心得: 1.其实此题有两种做法,动态规划,母函数.个人更喜欢使用动态规划来做,也可以直接套母函数的模板 Square Coins Time Limit: 2000/1000 MS (Java/Ot ...

  5. 划分型博弈型dp

    划分型动态规划: 513. Perfect Squares https://www.lintcode.com/problem/perfect-squares/description?_from=lad ...

  6. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

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

  8. PAT1048. Find Coins(01背包问题动态规划解法)

    问题描述: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...

  9. UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)

    UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...

随机推荐

  1. hashmap源码

    Java里各种基础容器的实现都是链表外加引用的形式.So,hashmap也不例外. 那小考虑下:hashmap是怎么产生的呢? 常用的两种数据结构数组和链表各有优劣,数组寻址容易,插入和删除困难:而链 ...

  2. PAT (Advanced Level) 1104. Sum of Number Segments (20)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  3. realypay

    1.判断新老接口的方法: 去网站后台接口配置的地方,如果有merchantno参数就是新接口,否则为老接口,新接口则不需要更新了. 2.老接口需要去网站后台卸载老的接口,然后再用新的(下载的文件)覆盖 ...

  4. Nginx代理外网映射

    外网映射内网端口8080, 外网访问使用端口8379: nginx监听8080和80端口 #user nobody; worker_processes ; #error_log logs/error. ...

  5. nested query for "pat2" table

    mysql> select t.appln_id, t.filing_date, t.appln_kind, t.people, GROUP_CONCAT(pu.publn_kind) from ...

  6. Angularjs循环二维数组

    <div ng-app> <div ng-controller="test"> <div ng-repeat="links in slide ...

  7. 使用curl获取乱码问题

    今天通过curl获取百度地图接口数据,获取到居然是乱码,于是我查看是不是编码问题,发现返回的编码和自己的编码都是utf-8, 继续找原因,发现header报文中  Content-encoding 为 ...

  8. 使用JQuery结合HIghcharts实现从后台获取JSON实时刷新图表

    项目做了一个报表,可以实时的观察呼叫中心的电话访问量,之前的版本是使用JFreechart做的,使用一段时间后出现内存溢出,服务器的内存使用量会变得很大,所以改用Ajax前台加载数据的方式实现实时报表 ...

  9. OpenGL学习--------动画制作

    假设某动画一共有n幅画面,则它的工作步骤就是:显示第1幅画面,然后等待一小段时间,直到下一个1/24秒显示第2幅画面,然后等待一小段时间,直到下一个1/24秒……显示第n幅画面,然后等待一小段时间,直 ...

  10. EL探索