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的串时候合法 转 ...
随机推荐
- BZOJ1807 : [Ioi2007]Pairs 彼此能听得见的动物对数
一维的情况: 排序后维护一个单调指针即可,时间复杂度$O(n\log n)$. 二维的情况: 旋转坐标系后转化为二维数点问题,扫描线+树状数组维护即可,时间复杂度$O(n\log n)$. 三维的情况 ...
- JAVA中堆栈和内存分配
(一).栈.堆 1.寄存器:最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制. 2. 栈:存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在堆(new 出来的对象)或 ...
- 在Eclipse中配置Tomcat 创建和运行Servlet/JSP
在Eclipse中配置Tomcat 创建和运行Servlet/JSP 步骤一:在Eclipse中配置Tomcat(注意下载Eclipse IDE for Java EE Developers) (1) ...
- hdu Line belt
这道题是一道3分搜索的题.其实这种题很多时候都出现在高中的解析几何上,思路很简单,从图中可以看到,肯定在AB线段和CD线段上各存在一点x和y使得所花时间最少 因为AB和CD上的时间与x和y点的坐标都存 ...
- thinkphp模板中foreach循环没数据的错误解决
从控制器方法中$this->assign();函数将值传递给html模板 但是模板不显示数据,直接出来的是代码,效果就和html中写了php代码不能解析一样. 原来是我将thinkphp框架的引 ...
- 使用ProxychainsMac下安装及配置
下面几种解决方式 一.先在VPS用composer把Laravel给拖回来,本地你就别想用Composer正常下载Laravel回来了 二.使用全局代理,暂时木有折腾过在终端下怎么折腾,GUI可以使用 ...
- 文件上传\">将在3秒钟后返回前页
conn.php: <?php $id=mysql_connect('localhost','root','root'); mysql_select_db("db_database12 ...
- java命令行参数
命令行参数就是main方法里面的参数String[] args他就是一个数组,args只是数据类型的一个名称,就是一个数组的变量,名称无所谓,类型没变就行了.这个就是程序的入口点.如图7.4所示: 图 ...
- CSS3 2D Transform
在 一个二维或三维空间,元素可以被扭曲.移位或旋转.只不过2D变形工作在X轴和Y轴,也就是大家常说的水平轴和垂直轴:而3D变形工作在X轴和Y轴之外, 还有一个Z轴.这些3D变换不仅可以定义元素的长度和 ...
- 解决Ecshop因为动态ip问题登录后台自动退出
解决Ecshop因为动态ip问题登录后台自动退出 PHP 铁匠 2年前 (2013-07-21) 1130℃ 0评论 修改lib_base.php文件real_ip()函数,添加以下代码即可解 ...