【动态规划】Gym - 101102A - Coins
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 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).
Output
For each test case, print the number of ways modulo 109 + 7 on a single line.
Example
2
4 3 5 18
2 3 4 1
10 5 5
2 1 20 20
10 30
50
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的更多相关文章
- Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
A - Coins Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descript ...
- Codeforces Gym - 101102A - Coins
A. Coins 题目链接:http://codeforces.com/gym/101102/problem/A time limit per test 3 seconds memory limit ...
- codeforce Gym 101102A Coins (01背包变形)
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
- 动态规划:HDU-1398-Square Coins(母函数模板)
解题心得: 1.其实此题有两种做法,动态规划,母函数.个人更喜欢使用动态规划来做,也可以直接套母函数的模板 Square Coins Time Limit: 2000/1000 MS (Java/Ot ...
- 划分型博弈型dp
划分型动态规划: 513. Perfect Squares https://www.lintcode.com/problem/perfect-squares/description?_from=lad ...
- 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 ...
- 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 ...
- PAT1048. Find Coins(01背包问题动态规划解法)
问题描述: Eva loves to collect coins from all over the universe, including some other planets like Mars. ...
- 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 ...
随机推荐
- mysql 查询 45 道题
一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...
- MySQL常用命令总结2
USE db_name; //使用(打开)数据库 SELECT DATABASE(); //查看当前打开的数据库 CREATE TABLE tb_name( column_name data_type ...
- Request 地址栏传值
request页面 protected void btnSearch_Click(object sender, EventArgs e) { Response.Redirect("Reque ...
- Super Mario
Super Mario Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 穿越泥地(mud)
穿越泥地(mud) 题目描述 清早6:00,FJ就离开了他的屋子,开始了他的例行工作:为贝茜挤奶.前一天晚上,整个农场刚经受过一场瓢泼大雨的洗礼,于是不难想象,FJ现在面对的 是一大片泥泞的土地.FJ ...
- PAT (Advanced Level) 1077. Kuchiguse (20)
最长公共后缀.暴力. #include<cstdio> #include<cstring> #include<cmath> #include<vector&g ...
- PHP实现畅言留言板和网易跟帖样式
原文:http://justcoding.iteye.com/blog/2251192 我要实现的就是下图的这种样式,可参考下面这两个网站的留言板,他们的实现原理都是一样的 http://chan ...
- opencv----(1) mat最好用,和IplImage,cvmat 比较
学习了几天,发现mat比IplImage,cvmat 好用太多了. 不知道确切的原文出处,我是转自新浪的一篇博文:http://blog.sina.com.cn/s/blog_534497fd0101 ...
- Gson通过借助TypeToken获取泛型参数的类型的方法
最近在使用Google的Gson包进行Json和Java对象之间的转化,对于包含泛型的类的序列化和反序列化Gson也提供了很好的支持,感觉有点意思,就花时间研究了一下. 由于Java泛型的实现机制,使 ...
- 笔记:利用Cocos2dx 3.3 lua 做一个动作类游戏(一)
在这之前,声明一下: 做不完我是小狗. 没办法,没毅力和恒心,之前的那个Quick Cocos2dx做的横版过关游戏的demo已经转成了3.3的版本了,其实也算是个半成品,战斗,UI啥的都有了,呵呵. ...