A - Coins

Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

standard input/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 ofM 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 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 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 109 + 7 on a single line.

Sample Input

Input
2
4 3 5 18
2 3 4 1
10 5 5
2 1 20 20
10 30
50
Output
2
0 题目链接:http://codeforces.com/gym/101102/problem/A题意:有两个序列分别有n和m个元素,现在要从两个序列中分别选出两个子集,设他们的和分别是S1和S2,现在使得选出来的结果满足以下两个条件
|S1-S2|<=K,S1+S2 = W;求有多少种选法,结果对1e9+7求余;
对于序列1,可以用dp[i][j]来表示前i个元素的子集构成和为 j 的方法数;那么就可以看成01背包来做即可;
dp[0] = 1;
dp[j] = (dp[j]+dp[j-a[i]])%mod;
两个序列处理完之后得到dp1和dp2,然后对应求结果即可;
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int N = ;
const double eps = 1e-;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+; int n, m, k, w;
int a[], b[];
LL dp1[N], dp2[N]; int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(dp1, , sizeof(dp1));
memset(dp2, , sizeof(dp2)); 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]); dp1[] = dp2[] = ; for(int i=; i<=n; i++)
{
for(int j=; j>=a[i]; j--)
dp1[j] = (dp1[j]+dp1[j-a[i]])%mod;
}
for(int i=; i<=m; i++)
{
for(int j=; j>=b[i]; j--)
dp2[j] = (dp2[j] + dp2[j-b[i]])%mod;
} LL ans = ; for(int i=; i<=w; i++)
{
int j=w-i;
if(max(i, j) - min(i, j) > k) continue;
ans = (ans + dp1[i]*dp2[j]%mod) % mod;
}
printf("%I64d\n", ans);
}
return ;
}

Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)的更多相关文章

  1. 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 ...

  2. Codeforces 2016 ACM Amman Collegiate Programming Contest B. The Little Match Girl(贪心)

    传送门 Description Using at most 7 matchsticks, you can draw any of the 10 digits as in the following p ...

  3. 2016 ACM Amman Collegiate Programming Contest D Rectangles

    Rectangles time limit per test 5 seconds memory limit per test 256 megabytes input standard input ou ...

  4. 18春季训练01-3/11 2015 ACM Amman Collegiate Programming Contest

    Solved A Gym 100712A Who Is The Winner Solved B Gym 100712B Rock-Paper-Scissors Solved C Gym 100712C ...

  5. 2015 ACM Amman Collegiate Programming Contest 题解

    [题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...

  6. 2017 ACM Amman Collegiate Programming Contest 题解

    [题目链接] A - Watching TV 模拟.统计一下哪个数字最多即可. #include <bits/stdc++.h> using namespace std; const in ...

  7. 2017 ACM Amman Collegiate Programming Contest

    A - Watching TV /* 题意:求出出现次数最多的数字 */ #include <cstdio> #include <algorithm> #include < ...

  8. gym100712 ACM Amman Collegiate Programming Contest

    非常水的手速赛,大部分题都是没有算法的.巨慢手速,老年思维.2个小时的时候看了下榜,和正常人差了3题(,最后还没写完跑去吃饭了.. A 水 Sort 比大小 /** @Date : 2017-09-0 ...

  9. ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)

    题目链接 https://vjudge.net/contest/240074#overview 只写一下自己做的几个题吧 /* D n^2的暴力dp怎么搞都可以的 这里先预处理 i到j的串时候合法 转 ...

随机推荐

  1. 移动WEBAPP开发常规CSS样式总结

    我所使用到的HTML页面标签: Section,div,artical,p,ol,ul,li,header,footer,span,form,input,label,h1,h2,h3 :详细说明我就不 ...

  2. c++ auto_ptr 智能指针

    c++使用智能指针应该保证无论在何种情况下,只要自己被摧毁,就一定连带释放其所有资源,而由于智能型指针本身就是区域变量, 所以无论是正常退出,还是异常退出,只要函数退出,它就一定销毁 常数型auto_ ...

  3. 【wikioi】1010 过河卒

    题目链接 算法:DFS+剪枝 14.01.02 PS: 递推应该也可以的,改天看看 刚开始最容易想到的是朴素搜索 #include <iostream> using namespace s ...

  4. javascript第三弹——数组

    什么是数组 数组是值的有序集合.每个值叫做元素,每个元素在数组中都有数字位置编号,也就是索引.JS中的数组是弱类型的,数组中可以含有不同类型的元素.数组元素甚至可以是对象或其它数组.数组的长度是动态的 ...

  5. Git Shell 安装版本

    #!/bin/sh v1.; do echo "Begin install Git $ver."; git reset --hard git clean -fdx git chec ...

  6. ASP.NET中Url重写后,打不开真正的Html页面

    不对IIS配置.html的映射,IIS站点目录下.html页面都能显示.当配置了.html的映射 IIS站点目录下真实存在的.html页面无法显示,错误信息:“页面无法显示”解决方法:1.首先照旧在网 ...

  7. iOS开发之正则表达式

    正则表达式用于字符串处理.表单验证等场合,实用高效.现将一些常用的表达式收集于此,以备不时之需. 匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹配中文还真是个头疼的事,有了这个表达 ...

  8. NSOperationQueue

    一.简介 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的.也可以将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步 ...

  9. StereoBM::disp12MaxDiff Crash the Release

    Initializing "cv::StereoBM bm.state->disp12MaxDiff" should be careful, inappropriate va ...

  10. Apache Spark源码走读之5 -- DStream处理的容错性分析

    欢迎转载,转载请注明出处,徽沪一郎,谢谢. 在流数据的处理过程中,为了保证处理结果的可信度(不能多算,也不能漏算),需要做到对所有的输入数据有且仅有一次处理.在Spark Streaming的处理机制 ...