1233 - Coin Change (III)

1233 - Coin Change (III)
PDF (English) Statistics Forum
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;
}


你可能感兴趣的:(dp)