【动态规划】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 ...
随机推荐
- APK瘦身
APK瘦身 主要从一下三方面来瘦身: 1. Java 源代码 1) ,这方面主要是通过最简洁的代码实现最直接的功能,还有就是提出上线前不必要的java代码,可以使用UCDector进行分析,从而对代码 ...
- PAT (Advanced Level) 1095. Cars on Campus (30)
模拟题.仔细一些即可. #include<cstdio> #include<cstring> #include<cmath> #include<algorit ...
- NPOI 2.0 教程(二):编辑既存的EXCEL文件
NPOI 2.0 教程(二):编辑既存的EXCEL文件 分类: C#技术 2014-03-11 15:40 993人阅读 评论(3) 收藏 举报 c#excelNPOI 转载请注明出处 http:// ...
- ibdata1文件--缩小mysql数据库的ibdata1文件
摘要 在MySQL数据库中,如果不指定innodb_file_per_table参数,单独存在每个表的数据,MySQL的数据都会存放在ibdata1文件. mysql ibdata1存放数据,索引等, ...
- apache禁止公网IP访问的配置
由于最近在配置负载均衡环境 外网使用Nginx做代理,分发到Apache服务器上,所以不希望Apache配置的服务被外网访问,只希望内网Nginx访问,所以只能使用局域网IP访问 配置方法: 打开Ap ...
- 过滤字符串html标签方法
过滤字符串html标签方法,如果输入的过滤标签为“*”,那么给字符串加上p标签 public static string noTagHtml(string str, string tagname) { ...
- 使用PowerDesigner画ER图详细教程
转:http://www.360doc.com/content/11/0624/15/2617151_129276457.shtml 一.概念数据模型概述数据模型是现实世界中数据特征的抽象.数据模型应 ...
- 关于 Equal Override Overload 和 IEquatable
namespace TestEqual { class Program { static void Main(string[] args) { Point2D a = new Point2D { X ...
- Ubuntu 12.04下安装thrift 0.9
Thrift这里就不介绍了,只说一句--Facebook很牛逼. 我这里安装Thrift主要是为Accumulo数据库作准备,所以java语言为必选项. 具体安装参考官方Apache Thrift R ...
- swift 自学小计
返回多个value func Myfunc()->(double,double,double) { return (3.14,2.33,9.88) } 动态参数 func Myfunc(numb ...