先上题目:

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. QT 相关资源(书籍、论坛、博客等。。。)整理

     QT 相关资源(书籍.论坛.博客等...)整理... 中文名:<提问的智慧> 英文名:How To Ask Questions The Smart Way 中文链接1:http://ww ...

  2. 一段程序的人生 第10章: server

    从第0章開始看 第拾章 server     一切基本安顿下来,我開始认真的检阅一下我所栖身的文件夹.来到了外面的大世界,果然不一样.越是细致查看越是认为这里真是一个再好只是的地方. 这个文件夹里面有 ...

  3. hdu 1035(DFS)

    Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  4. FFMS SQL文件执行错误

    [mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 [mysqld] #设置3306端口 port = 3306 # 设置mysql的安装目录 b ...

  5. Appium + python - long_press定位操作实例

    from appium.webdriver.common.touch_action import TouchActionfrom appium import webdriverimport timei ...

  6. layui富文本编译器后台获取图片路径

    @RequestMapping("add")    public ModelAndView add(News news){        ModelAndView mav = ne ...

  7. Elasticserach 同步索引报错:ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  8. 常用MIME类型(Flv,Mp4的mime类型设置)

    也许你会在纳闷,为什么我上传了flv或MP4文件到服务器,可输入正确地址通过http协议来访问总是出现“无法找到该页”的404错误呢?这就表明mp4格式文件是服务器无法识别的,其实,这是没有在iis中 ...

  9. 软件架构自学笔记——常见的软件架构(https://jiajunhuang.com/articles/2018_09_16-common_software_archtecture_pattern.md.html)

    分层模式 这种模式主要是将设计分层,每一层为其上层提供服务.例如:web开发中我们常常将某些常用的RESTful接口抽象出一个service层. 客户端-服务端模式 客户端和服务端分离,从而解耦.通过 ...

  10. intellij 创建java web项目(maven管理的SSH)

    intellij 创建java web项目(maven管理的SSH) 环境intellij IDEA14.MAVEN.Spring.Struts2.Hibernate.Java Web.工程搭建. 1 ...