博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Gym - 101102A - Coins
阅读量:7234 次
发布时间:2019-06-29

本文共 2492 字,大约阅读时间需要 8 分钟。

A. Coins
题目链接:
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
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.

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 1e9 + 7 on a single line.

Example
input
2 4 3 5 18 2 3 4 1 10 5 5 2 1 20 20 10 30 50
output
2 0 题目大意:分别求出两人硬币的部分和s1和s2,使得s1+s2=w,且|s1-s2|<=k,求出选取方法数。 方法:
由s1+s2=w,且|s1-s2|<=k得(w-k)/2=
<=(w+k)/2;
分别对两人的硬币选取方法进行01背包,然后遍历求和。 代码:
#include
#include
#include
using namespace std;#define ll long longconst int mod=1e9+7; const int N=155;const int W=15000+5;int a[N],b[N];int main(){ int t; cin>>t; while(t--) { int n,m,k,w; ll dp1[W]={ 1 },dp2[W]={ 1 }; scanf("%d %d %d %d",&n,&m,&k,&w); for(int i=0;i
=a[i];j--) { dp1[j]=(dp1[j]+dp1[j-a[i]])%mod; } } for(int i=0;i
=b[i];j--) { dp2[j]=(dp2[j]+dp2[j-b[i]])%mod; } } ll ans=0; for(int i=(w-k)/2+((w-k)%2?1:0);i<=(w+k)/2;i++)//注意下界 { ans=(ans+dp1[i]*dp2[w-i])%mod; } printf("%lld\n",ans); } return 0;}

 

转载于:https://www.cnblogs.com/widsom/p/6739950.html

你可能感兴趣的文章
每家企业应该知道的10家SaaS初创公司
查看>>
各种JDBC连接数据库的常用代码
查看>>
得大数据者得量化投资
查看>>
颠覆传统 微服务器剑指数据中心
查看>>
UCSB研发量子传感技术,具备纳米级别的空间分辨率
查看>>
智慧城市,美好生活刚开始
查看>>
混合云中如何阻断I/O瓶颈?
查看>>
Linux 64位操作系统安装配置java
查看>>
SolarCity欲为500万美国家庭搭建太阳能屋顶
查看>>
苹果进军印度市场到底有多难 连财政部长都不帮忙
查看>>
监控摄像机选型攻略之技术类型选用
查看>>
JAVA笔记——序列化
查看>>
《数据科学:R语言实现》——3.1 引言
查看>>
协作软件的前景、进展以及阵痛
查看>>
PyTorch 和 TensorFlow 哪个更好?看一线开发者怎么说
查看>>
怎么善于发现seo网站优化的问题?
查看>>
《Metasploit渗透测试手册》—第8章8.1节介绍
查看>>
《UG NX8.0中文版完全自学手册》一1.4 工具栏的定制
查看>>
合三为一,Linux 基金会欲打造顶级开源峰会
查看>>
《计算机系统:系统架构与操作系统的高度集成》——2.8 编译函数调用
查看>>