Gym 101102A Coins -- 2016 ACM Amman Collegiate Programming Contest(01背包变形)
Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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 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.
Sample Input
2
4 3 5 18
2 3 4 1
10 5 5
2 1 20 20
10 30
50
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背包变形)的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 2015 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Who Is The Winner 模拟. #include <bits/stdc++.h> using namespace std; int T; int n; s ...
- 2017 ACM Amman Collegiate Programming Contest 题解
[题目链接] A - Watching TV 模拟.统计一下哪个数字最多即可. #include <bits/stdc++.h> using namespace std; const in ...
- 2017 ACM Amman Collegiate Programming Contest
A - Watching TV /* 题意:求出出现次数最多的数字 */ #include <cstdio> #include <algorithm> #include < ...
- gym100712 ACM Amman Collegiate Programming Contest
非常水的手速赛,大部分题都是没有算法的.巨慢手速,老年思维.2个小时的时候看了下榜,和正常人差了3题(,最后还没写完跑去吃饭了.. A 水 Sort 比大小 /** @Date : 2017-09-0 ...
- ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)
题目链接 https://vjudge.net/contest/240074#overview 只写一下自己做的几个题吧 /* D n^2的暴力dp怎么搞都可以的 这里先预处理 i到j的串时候合法 转 ...
随机推荐
- BZOJ2757 : [SCOI2012]Blinker的仰慕者
BZOJ AC900题纪念~~ 若K>0,则 设f[i][j]表示i位数字,积为j的数字的个数 g[i][j]表示i位数字,积为j的数字的和 DP+Hash预处理 查询时枚举LCP然后统计贡献 ...
- 【BZOJ】1458: 士兵占领(上下界网络流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1458 是不是我脑洞太小了.......直接弄上下界最小流........(就当复习了.. 二分图X和 ...
- react-amazeui-touch 妹子Ui移动端学习
必须先配置并且学习react环境: http://www.cnblogs.com/CyLee/p/5668373.html 官方地址 http://t.amazeui.org/#/docs/form? ...
- diff和patch配合使用(转载备用)
Linux下diff与patch命令的配合使用 在Linux下,diff与patch命令配合使用可以进行简单的代码维护工作. [A] diffdiff命令用于比较文件的差异,可以用于制作patch文件 ...
- Trie
字典树 class Trie { public: Trie() { root = new Node(); } ~Trie() { destroy(root); } void insert(string ...
- Redhat系列Linux的基础命令
为网卡配置静态IP地址vi /etc/sysconfig/network-scripts/ifcfg-eth0DEVICE=eth0ONBOOT=yesBOOTPROTO=staticHWADDR=0 ...
- hudson安装
Hudson 只是一个持续集成服务器(持续集成工具),要想搭建一套完整的持续集成管理平台, SVN.Maven.Sonar 等工具按需求整合则可. 1. 安装 JDK 并配置环境变量(略) J ...
- HDU 1075 What Are You Talking About(Trie的应用)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- CSS3 2D Transform
在 一个二维或三维空间,元素可以被扭曲.移位或旋转.只不过2D变形工作在X轴和Y轴,也就是大家常说的水平轴和垂直轴:而3D变形工作在X轴和Y轴之外, 还有一个Z轴.这些3D变换不仅可以定义元素的长度和 ...
- 如何设置jquery的ajax方法为同步
jax请求默认的都是异步的如果想同步 async设置为false就可以(默认是true) var html = $.ajax({ url: "some.php", async: ...