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. Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践

    Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践 Spring Boot + Nginx + Mysql 是实际工作中 ...

  2. SaaS教父:我眼中最糟糕的9条SaaS建议(转)

    文章摘要:美国SaaS行业的教父级人物Jason Lemkin近日总结了在他眼里最糟糕的9条SaaS方面的建议,希望对SaaS行业的创业者有所启发. SaaS行业的创业者平时肯定会收到外界各种各样的建 ...

  3. Deep Learning Terminologies

    Deep Learning Terminologies batch full batch 已知在梯度下降中,需要对所有样本进行处理过后然后走一步(梯度更新),那么如果我们的样本规模的特别大的话效率就会 ...

  4. Bugku-CTF之web基础$_GET

    Day3   web基础$_GET   http://123.206.87.240:8002/get/   打开之后是一段代码  

  5. 字体图标,盒子显隐,overflow属性,伪类设计边框,盒子阴影2d形变

    字体图标 ''' fa框架: http://fontawesome.dashgame.com/ 下载 => 引入css文件 引入字体图标库 <link rel="styleshe ...

  6. day 26 元类

    一.isinstance issubclass class Person: passclass Student(Person): passstu1=Student()#判断是不是实例print(isi ...

  7. Redis 安装,配置以及数据操作

    Nosql介绍 Nosql:一类新出现的数据库(not only sql)的特点 不支持SQL语法 存储结构跟传统关系型数据库中那种关系表完全不同,nosql中存储的数据都是k-v形式 Nosql的世 ...

  8. luogu P5105 不强制在线的动态快速排序

    前言 考试的时候居然想错了区间贡献,mdzz 思路 题目看着很方啊,难道要树套树? 但数据范围提醒我们,是nlogn的复杂度 Sort(S)的定义是不是很鬼畜 但我们不动脑子的打表容易发现 连续区间[ ...

  9. SpringBoot 使用Sharding-JDBC进行分库分表及其分布式ID的生成

    为解决关系型数据库面对海量数据由于数据量过大而导致的性能问题时,将数据进行分片是行之有效的解决方案,而将集中于单一节点的数据拆分并分别存储到多个数据库或表,称为分库分表. 分库可以有效分散高并发量,分 ...

  10. volatile 变量使用

    1,对其它线程可见性.原理是:别的线程每次使用前都是要刷新一下值,并不是原子性同步.所有还是会出现线程不安全. 2,禁止指令重新排序.也就是会出现机器实际执行可能和代码的顺序不一样.使用volatil ...