A. Coins
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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 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 1e9 + 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
题目大意:分别求出两人硬币的部分和s1和s2,使得s1+s2=w,且|s1-s2|<=k,求出选取方法数。
方法:
由s1+s2=w,且|s1-s2|<=k得(w-k)/2=<s1<=(w+k)/2;
分别对两人的硬币选取方法进行01背包,然后遍历求和。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
const int mod=1e9+;
const int N=;
const int W=+;
int a[N],b[N];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m,k,w;
ll dp1[W]={ },dp2[W]={ };
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);
for(int i=;i<n;i++)
{
for(int j=w;j>=a[i];j--)
{
dp1[j]=(dp1[j]+dp1[j-a[i]])%mod;
}
}
for(int i=;i<m;i++)
{
for(int j=w;j>=b[i];j--)
{
dp2[j]=(dp2[j]+dp2[j-b[i]])%mod;
}
}
ll ans=;
for(int i=(w-k)/+((w-k)%?:);i<=(w+k)/;i++)//注意下界
{
ans=(ans+dp1[i]*dp2[w-i])%mod;
}
printf("%lld\n",ans);
}
return ;
}

Codeforces 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. 【动态规划】Gym - 101102A - Coins

    Hasan and Bahosain want to buy a new video game, they want to share the expenses. Hasan has a set of ...

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

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

  4. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  5. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  6. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  7. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

  8. Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】

     2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...

  9. codeforces gym 100553I

    codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...

随机推荐

  1. 统计py文件中的代码行

    希望是输入一个合法的文件夹的路径,然后代码自动读取该文件夹下的每个py结尾的文件内的代码行数,最后汇总一个数,但现在只是有思路,却没时间写,这是能读取同级文件下的某个文件, with open('te ...

  2. 前端诡异参数start

    今天搞了一下午只为一个诡异事件:前端请求数据时总是会传一个start=20的参数,二搜索整个项目也找不出这个start的踪影. 倒是搜到一个pageLength的参数是20,但就是找不到start. ...

  3. oracle exadata一体机虚拟机

    14年参加partner培训的时候,拿了份oracle exadata一体机虚拟机,有兴趣的可以试试,不过比较大,压缩后10GB,解压后50GB,启动后直接可用,2RAC节点+1存储节点,环境最好内存 ...

  4. 写给大忙人的ELK最新版6.2.4学习笔记-Logstash和Filebeat解析(java异常堆栈下多行日志配置支持)

    接前一篇CentOS 7下最新版(6.2.4)ELK+Filebeat+Log4j日志集成环境搭建完整指南,继续对ELK. logstash官方最新文档https://www.elastic.co/g ...

  5. 01:CENTOS使用VIRTUALENV搭建独立的PYTHON环境-PYTHON虚拟环境

    1.1 安装virtualenv环境 https://www.cnblogs.com/liuyansheng/p/6141197.html 1.安装virtualenv yum install pyt ...

  6. 20145208 蔡野 《网络对抗》Exp9 web安全基础实践

    20145208 蔡野 <网络对抗>Exp9 web安全基础实践 本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 实验后回答问题 (1)SQL注入攻击原理,如何 ...

  7. 牛客网数据库SQL实战(21-25)

    21.查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序CREATE TABLE `employees` (`emp_no` i ...

  8. uniGUI试用笔记(九)

    uniGUI执行程序部署有3种形式 1.ISAPI模式 部署在IIS或Apache,程序编译为Dll形式,没有试,准备后续专门测试一下. 2.标准执行文件模式 将软件编译成一个独立的Exe文件,包括了 ...

  9. POP-OOP-SOP-COP-SOA-AOP

    一.面向过程 二.面向对象 三.面向服务 四.补充 五.总结: 英文及缩写: 面向过程:procedure oriented programming POP 面向对象:object oriented ...

  10. php文档注释提取工具phpdocumentor的使用

    phpDocumentor, 分为文档性注释, 和 非文档性注释; 命令为: phpdoc -h, -f, -d.... 提取/ 生成 程序的注释文档, 实际上有很多种工具, 如: doc++, do ...