Codeforces Gym - 101102A - Coins
3 seconds
256 megabytes
standard input
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.
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 N, M, K 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).
For each test case, print the number of ways modulo 1e9 + 7 on a single line.
2
4 3 5 18
2 3 4 1
10 5 5
2 1 20 20
10 30
50
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的更多相关文章
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
A - Coins Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descript ...
- 【动态规划】Gym - 101102A - Coins
Hasan and Bahosain want to buy a new video game, they want to share the expenses. Hasan has a set of ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
- 【Codeforces Gym 100725K】Key Insertion
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
- 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 ...
- codeforces gym 100553I
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
随机推荐
- Jira客户端
github地址 https://github.com/rcarz/jira-client 使用 1)私服 <!-- https://mvnrepository.com/artifact/net ...
- Solr基本操作
/update 使用/update进行索引维护,进入Solr管理界面SolrCore下的Document下: 我们进行更新操作可以用json和xml多种格式,这里以xml格式为例说明.先来看看界面上的 ...
- socket之 select模型
前段时间一直想学习网络编程的select模型,看了<windows网络编程>的介绍,参考了别人的博客. 这里的资料主要来自http://www.cnblogs.com/RascallySn ...
- 1.多表查询 => 转化为一张联合大表 2.可视化工具 3.pymysql模块
多表数据 create table dep( id int primary key auto_increment, name varchar(16), work varchar(16) ); crea ...
- Git clone、git reset
一,git clone 1,git clone某一个分支 git clone -b <branch> <remote_repo> 2,.git 文件太大 :clone的时候,可 ...
- python --- 21 MRO C3算法
一.python2.2之前用的是 经典类的MRO继承 ①深度递归继承 从左到右 ,一条路走到黑 ②广度继承 一层一层的继承 深度继承时 为 R 1 2 3 4 ...
- 最大公约数gcd与最小公倍数lcm
最大公约数:gcd 最大公倍数:lcm gcd和lcm的性质:(我觉得主要是第三点性质) 若gcd (
- MD5+salt 工具类
import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.securit ...
- Excel lastindex of a substring
I think I get what you mean. Let's say for example you want the right-most \ in the following string ...
- P3211 [HNOI2011]XOR和路径
思路 看到异或,容易联想到二进制位之间是相互独立的,所以可以把问题变成每个二进制位为1的概率再乘上(1<<pos)的值 假设现在考虑到pos位,设f[i]为第i个节点期望的异或和第pos位 ...