zoukankan      html  css  js  c++  java
  • 【动态规划】Gym

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

    Example

    Input
    2
    4 3 5 18
    2 3 4 1
    10 5 5
    2 1 20 20
    10 30
    50
    Output
    2
    0

    对A和B的硬币分别dp,f[i]表示拼成i元的方案数,for i=1 to n for j=15000 down to 0 f(j+a(i))+=f(j)

    最后枚举一下差在K以内,且i+j=W的f(i)和g(j)即可。

    #include<cstdio>
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    #define MOD 1000000007ll
    int T,n,m,K,W;
    int a[160],b[160];
    int Abs(int x)
    {
    	return x<0 ? (-x) : x;
    }
    ll f[15010],g[15010];
    int main()
    {
    	scanf("%d",&T);
    	for(;T;--T)
    	  {
    	  	scanf("%d%d%d%d",&n,&m,&K,&W);
    		for(int i=1;i<=n;++i)
    	  	  scanf("%d",&a[i]);
    		for(int i=1;i<=m;++i)
    	  	  scanf("%d",&b[i]);
    	  	memset(f,0,sizeof(f));
    	  	memset(g,0,sizeof(g));
    	  	f[0]=1;
    	  	for(int i=1;i<=n;++i)
    	  	  for(int j=15000;j>=0;--j)
    	  	    if(a[i]+j<=15000)
    	  	      f[j+a[i]]=(f[j+a[i]]+f[j])%MOD;
    	  	g[0]=1;
    	  	for(int i=1;i<=m;++i)
    	  	  for(int j=15000;j>=0;--j)
    	  	    if(b[i]+j<=15000)
    	  	      g[j+b[i]]=(g[j+b[i]]+g[j])%MOD;
    	  	ll ans=0;
    	  	for(int i=0;i<=W;++i)
    	  	  if(Abs(W-i-i)<=K)
    	  	    ans=(ans+f[i]*g[W-i]%MOD)%MOD;
    	  	cout<<ans<<endl;
    	  }
    	return 0;
    }
  • 相关阅读:
    普通文本输入数学符号的方式
    Chrome crx离线插件下载及安装
    Solidworks常见问题一览
    数学学术资源站点(zz)
    最难读的20个英文单词
    运用html5 canvas做飞机大战游戏(2)
    html
    运用html5 canvas做飞机大战游戏(1)
    js
    css
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/6287950.html
Copyright © 2011-2022 走看看