1231 - Coin Change (I) 多重背包--背包计数

本文介绍了一个基于多重背包思想的问题:在一个特殊的商店里,有n种不同面值的硬币,每种硬币的数量有限,求组合成特定金额的方法数量。通过示例详细解释了输入输出格式,并提供了一个C++实现代码。

In a strange shop there are n types of coins of value A1, A2 ... AnC1, C2, ... Cn denote the number of coins of value A1, A2 ... An respectively. You have to find the number of ways you can make K using the coins.

For example, suppose there are three coins 1, 2, 5 and we can use coin 1 at most 3 times, coin 2 at most 2 times and coin 5 at most 1 time. Then if K = 5 the possible ways are:

1112

122

5

So, 5 can be made in 3 ways.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing two integers n (1 ≤ n ≤ 50) and K (1 ≤ K ≤ 1000). The next line contains 2n integers, denoting A1, A2 ... An, C1, C2 ... Cn (1 ≤ Ai ≤ 100, 1 ≤ Ci ≤ 20). All Ai will be distinct.

Output

For each case, print the case number and the number of ways K can be made. Result can be large, so, print the result modulo 100000007.

Sample Input

Output for Sample Input

2

3 5

1 2 5 3 2 1

4 20

1 2 3 4 8 4 2 1

Case 1: 3

Case 2: 9

 


PROBLEM SETTER: JANE ALAM JAN

 

利用多重背包思想。

#include<bits/stdc++.h>
# define ll long long
using namespace std;
const ll mod=100000007;
const int N=1e4;
int num[N];
int a[N];//价值
ll dp[N];
int main()
{
    int t;
    cin>>t;
    for(int o=1;o<=t;o++)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++)cin>>a[i];
        for(int i=1;i<=n;i++)cin>>num[i];
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for(int i=1;i<=n;i++)
        {
            //n件
            for(int j=m;j>=a[i];j--)
            {
                for(int k=0;k<=num[i];k++)//0-num[i]件件数
                {
                    if(j-k*a[i]<0)break;
                    if(k==0)continue;
                    dp[j]=dp[j]+dp[j-k*a[i]]%mod;//取或不取

                }
            }
        }
        printf("Case %d: %d\n",o,dp[m]%mod);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值