先上题目:

1231 - Coin Change (I)
Time Limit: 1 second(s) Memory Limit: 32 MB

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

  题意:有n种硬币,每种硬币有一定的数量以及价值,给你一个数k,问你有多少种方法可以用这些硬币凑出k。

  背包问题,是一个多重背包,因为同一种硬币的数量上限比较少,所以可以直接枚举同一种硬币不同数量的情况。

  dp[i][j]的含义:前i种银币可以凑出j的方法有多少种?

  对于第i种硬币,初始化的时候是dp[i][l*a[i]]=1 意思是对于第i种硬币,如果同时选l个硬币的时候l*a[i]<=k那么就有一种方法。

  那么状态转移方程就是dp[i][j]=(dp[i][j]%MOD+dp[i-1][j-l*a[i]]%MOD)%MOD 意思就是对于前i种硬币的方法来自两部分,①第i种硬币自己本身凑出来了,②用l个第i种硬币加上前面凑出j-l*a[i]这么多钱的方法数量。

  这种背包和之前做的那些问题相比,最优子结构需要更多的分析才能发现。看来DP还要努力啊。

上代码:

 #include <cstdio>
#include <cstring>
#define MAX 52
#define MAXN 1002
#define MOD 100000007
using namespace std; int a[MAX];
int c[MAX];
int dp[MAX][MAXN]; int main()
{
int t,n,k;
//freopen("data.txt","r",stdin);
scanf("%d",&t);
for(int u=;u<=t;u++){
scanf("%d %d",&n,&k);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=;i<=n;i++){
scanf("%d",&c[i]);
}
for(int i=;i<=n;i++){
for(int j=;j<=c[i];j++){
if(j*a[i]<=k){
dp[i][j*a[i]]=;
}
}
}
for(int i=;i<=n;i++){
for(int j=;j<=k;j++){
for(int l=;l<=c[i];l++){
if(l*a[i]<j){
dp[i][j]=(dp[i][j]%MOD+dp[i-][j-l*a[i]]%MOD)%MOD;
}
}
}
}
printf("Case %d: %d\n",u,dp[n][k]%MOD);
}
return ;
}

1231

LightOJ - 1231 - Coin Change (I)的更多相关文章

  1. Lightoj 1231 - Coin Change (I) (裸裸的多重背包)

    题目链接: Lightoj  1231 - Coin Change (I) 题目描述: 就是有n种硬币,每种硬币有两个属性(价值,数目).问用给定的硬币组成K面值,有多少种方案? 解题思路: 赤果果的 ...

  2. LightOJ - 1232 - Coin Change (II)

    先上题目: 1232 - Coin Change (II)   PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: ...

  3. LightOJ 1235 - Coin Change (IV) (折半枚举)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1235 题目描述: 给出n个硬币,每种硬币最多使用两次,问能否组成K面值? 解 ...

  4. Lightoj 1235 - Coin Change (IV) 【二分】

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1235 题意: 有N个硬币(N<=18).问是否能在每一个硬币使用不超过两 ...

  5. C - Coin Change (III)(多重背包 二进制优化)

    C - Coin Change (III) Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  6. [LeetCode] Coin Change 硬币找零

    You are given coins of different denominations and a total amount of money amount. Write a function ...

  7. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. HDU 2069 Coin Change

    Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. UVA 674 Coin Change(dp)

    UVA 674  Coin Change  解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...

随机推荐

  1. luogu3157 动态逆序对

    题目大意 给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序对数. 思路 #include <cstdio> #include <c ...

  2. luogu2828 [HEOI2016/TJOI2016]排序

    题目大意 给出一个1到n的全排列,现在对这个全排列序列进行m次局部排序,排序分为两种:(0,l,r)表示将区间[l,r]的数字升序排序:(1,l,r)表示将区间[l,r]的数字降序排序.最后询问第q位 ...

  3. DataGuard总体结构

    一.DataGuard总体结构 总体目标 1.   描述计划和非计划停机的不同因数 2.   DataGuard的主要组件 3.   物理以及逻辑DataGuard的异同 4.   建立DataGua ...

  4. B1003 物流运输(最短路 + dp)

    这个dp其实不是那么难,状态其实很好想,但是细节有少许偏差. 当时我并没有想到最短路要在dp之外写,后来看题解之后发现要预处理出来每段时间1~M的最短路,然后直接dp. 题目: Description ...

  5. Vue发布过程中遇到坑,以及webpack打包优化

    前言 这段时间,本人自己做了一个vue画面部署到自己的服务器上,发现运行速度慢的的惊人,虽然服务器很渣(本人没什么钱,只能租最差的服务器,主要是给自己学习用的),但是这样开发出来的网站简直不能用,所以 ...

  6. POJ 1149 PIGS (AC这道题很不容易啊)网络流

    PIGS Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlo ...

  7. 关于sizeof()、size()的有些问题

    #include<iostream>using namespace std; int main() { char a[] = "abcdefg"; string s = ...

  8. Django学习案例一(blog):四. 使用Admin

    1. 创建超级用户 python manage.py createsuperuser 创建过程中输入用户名,并设定密码(记住). 后台管理汉化.修改settings.py中LANGUAGE_CODE ...

  9. 实现Brush对象的五种图形

    本实例将使用Graphics类绘制五种图形来分别演示SolidBrush.HatchBrush.TextureBrush.LinearGradientBrush.PathGradientBrush这五 ...

  10. jqurey事件 ready方法用法

    ready 在文档加载后激活函数 例: <html> <head> <script type="text/javascript" src=" ...