先上题目:

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. 牛逼的postman,分类管理更好用

  2. Python入门 来点栗子

    查天气(1) http://wthrcdn.etouch.cn/weather_mini?citykey=101280804 http://wthrcdn.etouch.cn/WeatherApi?c ...

  3. [Apple开发者帐户帮助]三、创建证书(6)创建创建VoIP服务证书

    VoIP:基于IP的语音传输(英语:Voice over Internet Protocol,缩写为VoIP)是一种语音通话技术,经由网际协议(IP)来达成语音通话与多媒体会议,也就是经由互联网来进行 ...

  4. [Apple开发者帐户帮助]三、创建证书(4)创建Safari签名证书

    您的Safari扩展程序必须由Apple颁发的证书签名,您可以在开发者帐户中创建和下载该证书. 在“ 证书”,“标识符和配置文件”中,从左侧的弹出菜单中选择“Safari扩展”. 在“证书”下,选择“ ...

  5. [Apple开发者帐户帮助]三、创建证书(3)创建企业分发证书

    作为Apple Developer Enterprise Program的成员,您可以创建多个企业分发证书. 所需角色:帐户持有人或管理员. 在证书,标识符和配置文件中,从左侧的弹出菜单中选择iOS, ...

  6. idea新建文件无法识别(二)

    1,出现问题的步骤: 当想新增一个Hello.java文件时候 选错了文件类型.新建了一个text文件.当回过头来把文件后缀改掉的时候发现idea无法识别,颜色一直是灰色. 2,解决问题的办法: 选择 ...

  7. 显示程序输出并复制到文件(tee 命令)

    Linux tee命令用于读取标准输入的数据,并将其内容输出成文件. tee指令会从标准输入设备读取数据,将其内容输出到标准输出设备,同时保存成文件. 语法 tee [-ai][--help][--v ...

  8. django模型层(二)

    多表操作 创建模型 实例:我们来假定下面这些概念,字段和关系 作者模型:一个作者有姓名和年龄. 作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之间是一对 ...

  9. 【Leetcode】376. Wiggle Subsequence

    Description: A sequence of numbers is called a wiggle sequence if the differences between successive ...

  10. 使用Attiny 85开发板制作BadUSB

    什么是BadUSB?请查看:http://www.baike.com/wiki/BadUSB 或者看看腾讯这个视频!https://v.qq.com/x/page/l01425u2igw.html   ...