先上题目:

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. luogu3382【模板】三分法

    给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减.试求出x的值. 看代码即可. #include <cstdio> #include < ...

  2. 使用U-Boot的TFTP(远程/网络内核)

    前提条件 假设您的主机PC运行的是Ubuntu 14.04.1 LTS或更高版本,并且与您的开发平台在同一个本地网络上;为了简单起见,我们假设网络上也有DHCP服务器.如果使用Juno,请确保使用的是 ...

  3. B1218 [HNOI2003]激光炸弹 dp

    这个题其实打眼一看就知道差不多是dp,而且基本确定是前缀和.然后硬钢就行了...直接暴力预处理前缀和,然后直接dp就行. 题干: Description 一种新型的激光炸弹,可以摧毁一个边长为R的正方 ...

  4. JFreeChart之堆叠柱形图(StackedBar)

    JFreeChart之堆叠柱形图(StackedBar) JAVA JFreeChart 最近的项目使用有个功能需要使用到堆叠柱形图,看了项目以前的代码实现没有想要的结果.所以自己就先到官网下载了 D ...

  5. 移动App服务端架构设计

      我从事手机app服务端开发现在已经是3个年头,自己也整理出了一套相对好用的服务架构,写出来,跟大家一起分享.如有不足,还请多指教. 一:基础流程图. 其实有一点还需要加上,就是对json的压缩和加 ...

  6. c#调用带有自定义表结构的存储过程

    1.新建自定义表结构 CREATE TYPE [dbo].[HBForHBGHDR] AS TABLE( [序号] [int] NULL, [客户编号] [varchar](15) NULL ) GO ...

  7. 企业级分布式监控系统--zabbix

    目录 1.Zabbix简介 2.zabbix安装 3.工作原理 4.监控功能 5.监控系统架构 6.Zabbix系统架构 7.Zabbix组件构成 8.zabbix监控环境中基本概念 正文 回到顶部 ...

  8. Spring logger 配置

    1. logback-spring.xml <?xml version="1.0" encoding="UTF-8"?> <configura ...

  9. Mysql Workbench 执行sql语句删除数据时提示error code 1175

    error code 1175是因为有安全模式限制 执行命令SET SQL_SAFE_UPDATES = 0;之后可以进行操作

  10. 【Oracle】设置快速恢复区及reset快速恢复区

    快速恢复区 概念 是一个默认放置所有备份恢复操作有关文件的地方,包括:控制文件在线镜像.在线重做日志.归档日志.外来归档日志.控制文件镜像复制.数据文件镜像复制.RMAN备份片和闪回日志. 如果启用的 ...