1233 - Coin Change (III)

1233 - Coin Change (III)
Time Limit: 2 second(s)Memory Limit: 32 MB

In a strange shop there are n types of coins of valueA1, A2 ... An. C1, C2,... Cn denote the number of coins of value A1, A2... An respectively. You have to find the number of differentvalues (from 1 to m), which can be produced using these coins.

Input

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

Each case starts with a line containing two integers n (1≤ n ≤ 100), m (0 ≤ m ≤ 105). The nextline contains 2n integers, denoting A1, A2 ...An, C1, C2 ... Cn (1 ≤ Ai≤ 105, 1 ≤ Ci ≤ 1000). All Aiwill be distinct.

Output

For each case, print the case number and the result.


Sample Input

2

3 10

1 2 4 2 1 1

2 5

1 4 2 1

Output for Sample Input

Case 1: 8

Case 2: 4

题意: 给出n中硬币的币值,每种硬币各有c[i]个,问1~m中有多少个能够由这n种硬币构成

题解: 这道题可以用多重背包做,也可以用完全背包做,不过要加一个计数器,保证取得个数不超过c[i]个,dp[i]表示能不能组成i,b[i]表示组成i的用了多少个a[i];


AC代码:

/* ***********************************************
Author        :xdlove
Created Time  :2015年11月04日 星期三 12时12分04秒
File Name     :DP/1233_Coin_Change_III.cpp
 ************************************************ */

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

using namespace std;


#define REP_ab(i,a,b) for(int i = a; i <= b; i++)
#define REP(i, n) for(int i = 0; i < n; i++)
#define REP_1(i,n) for(int i = 1; i <= n; i++)
#define DEP(i,n) for(int i = n - 1; i >= 0; i--)
#define DEP_N(i,n) for(int i = n; i >= 1; i--)
#define CPY(A,B) memcpy(A,B,sizeof(B))
#define MEM(A) memset(A,0,sizeof(A))
#define MEM_1(A) memset(A,-1,sizeof(A))
#define MEM_INF(A) memset(A,0x3f,sizeof(A))
#define MEM_INFLL(A) memset(A,0x3f3f,sizeof(A))
#define mid (((l + r) >> 1))
#define lson l, mid, u << 1
#define rson mid + 1, r, u << 1 | 1
#define ls (u << 1)
#define rs (u << 1 | 1)


typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e5 + 5;
const int MAXM = MAXN;
const int mod = 1e9 + 7;

int a[110],c[110],b[MAXN];
bool dp[MAXN];
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T,n,m,cnt = 0;
    cin>>T;
    while(T--)
    {
        scanf("%d %d",&n,&m);
        REP(i,n) scanf("%d",&a[i]);
        REP(i,n) scanf("%d",&c[i]);
        MEM(dp);
        dp[0] = true;
        REP(o,n)
        {
            MEM(b);
            REP_ab(i,a[o],m) 
            {
                if(dp[i] || b[i - a[o]] >= c[o]) continue;
                dp[i] |= dp[i - a[o]];
                b[i] = b[i - a[o]] + 1;
            }
        }
        int num = 0;
        REP_1(i,m)
            num += dp[i];
        printf("Case %d: %d\n",++cnt,num);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值