Codeforces Gym - 101102A - Coins
3 seconds
256 megabytes
standard input
standard 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 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 S1 and Bahosain can choose a subset of sum S2such that S1 + S2 = W and |S1 - S2| ≤ K.
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).
For each test case, print the number of ways modulo 1e9 + 7 on a single line.
2
4 3 5 18
2 3 4 1
10 5 5
2 1 20 20
10 30
50
2
0
题目大意:分别求出两人硬币的部分和s1和s2,使得s1+s2=w,且|s1-s2|<=k,求出选取方法数。
方法:
由s1+s2=w,且|s1-s2|<=k得(w-k)/2=<s1<=(w+k)/2;
分别对两人的硬币选取方法进行01背包,然后遍历求和。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
const int mod=1e9+;
const int N=;
const int W=+;
int a[N],b[N];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m,k,w;
ll dp1[W]={ },dp2[W]={ };
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);
for(int i=;i<n;i++)
{
for(int j=w;j>=a[i];j--)
{
dp1[j]=(dp1[j]+dp1[j-a[i]])%mod;
}
}
for(int i=;i<m;i++)
{
for(int j=w;j>=b[i];j--)
{
dp2[j]=(dp2[j]+dp2[j-b[i]])%mod;
}
}
ll ans=;
for(int i=(w-k)/+((w-k)%?:);i<=(w+k)/;i++)//注意下界
{
ans=(ans+dp1[i]*dp2[w-i])%mod;
}
printf("%lld\n",ans);
}
return ;
}
Codeforces 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 ...
 - 【动态规划】Gym - 101102A - Coins
		
Hasan and Bahosain want to buy a new video game, they want to share the expenses. Hasan has a set of ...
 - codeforce  Gym 101102A Coins (01背包变形)
		
01背包变形,注意dp过程的时候就需要取膜,否则会出错. 代码如下: #include<iostream> #include<cstdio> #include<cstri ...
 - Codeforces Gym 101252D&&floyd判圈算法学习笔记
		
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
 - Codeforces Gym 101190M Mole Tunnels - 费用流
		
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
 - Codeforces Gym 101623A - 动态规划
		
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
 - 【Codeforces Gym 100725K】Key Insertion
		
Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...
 - Codeforces gym 101343 J.Husam and the Broken Present 2【状压dp】
		
2017 JUST Programming Contest 2.0 题目链接:Codeforces gym 101343 J.Husam and the Broken Present 2 J. Hu ...
 - codeforces gym 100553I
		
codeforces gym 100553I solution 令a[i]表示位置i的船的编号 研究可以发现,应是从中间开始,往两边跳.... 于是就是一个点往两边的最长下降子序列之和减一 魔改树状数 ...
 
随机推荐
- CSM与UEFI
			
最近公司产品部购置一批新电脑,但是预装的win10不能保证兼容老平台软件,于是安装win7系统的任务就落到了我的手中. 观察参数,是8代的U,产品说运维说无能为力,装不了win7.我在网上搜了一下,是 ...
 - sync—WaitGroup
			
用途:阻塞主线程的执行,直到所有的goroutine执行完成 WaitGroup总共有三个方法:Add(delta int),Done(),Wait().简单的说一下这三个方法的作用. Add:添加或 ...
 - keepalived的原理以及配置使用详解
			
一.vrrp协议简介 VRRP(Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议. VRRP协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路 ...
 - mint-ui之tabbar使用
			
<template> <div> <!-- tabcontainer --> <mt-tab-container class="page-tabba ...
 - python --- 09          初始函数      参数
			
函数 1.函数: 对代码块和功能的封装和定义 2.格式及语法 def 函数名() # 定义 函数体 函数名() # 调用 3. return ret ...
 - linux基础之网络基础配置
			
基础命令:ifconfig/route/netstat,ip/ss,nmcli 一.ifconfig/route/netstat相关命令 1. ifconfig - configure a netw ...
 - SpringBoot 使用Sharding-JDBC进行分库分表及其分布式ID的生成
			
为解决关系型数据库面对海量数据由于数据量过大而导致的性能问题时,将数据进行分片是行之有效的解决方案,而将集中于单一节点的数据拆分并分别存储到多个数据库或表,称为分库分表. 分库可以有效分散高并发量,分 ...
 - Spyder 调出绘图界面
			
Tools->Preference->IPython console->Graphics->Graphics backend->QT4 or QT5
 - Gym 101775J Straight Master(差分数组)题解
			
题意:给你n个高度,再给你1~n每种高度的数量,已知高度连续的3~5个能消去,问你所给的情况能否全部消去:例:n = 4,给出序列1 2 2 1表示高度1的1个,高度2的2个,高度3的2个,高度4的1 ...
 - 【Spring Security】一、快速入手
			
一 概要 Spring Security,这是一种基于 Spring AOP 和 Servlet 过滤器的安全框架.它提供全面的安全性解决方案,同时在 Web 请求级和方法调用级处理身份确认和授权.这 ...