LightOJ

先上题目:

1232 - Coin Change (II)
Time Limit: 1 second(s) Memory Limit: 32 MB

In a strange shop there are n types of coins of value A1, A2 ... An. You have to find the number of ways you can make K using the coins. You can use any coin at most K times.

For example, suppose there are three coins 1, 2, 5. Then if K = 5 the possible ways are:

11111

1112

122

5

So, 5 can be made in 4 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 ≤ 100) and K (1 ≤ K ≤ 10000). The next line contains n integers, denoting A1, A2 ... An (1 ≤ Ai ≤ 500). 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

4 20

1 2 3 4

Case 1: 4

Case 2: 108

   题意:有n种硬币,各有其价值,给你一个数k,问你有多少种方法可以用这n种硬币凑出k,其中每种硬币最多可以用k个。

   根据题意分析,可以发现这是一条完全背包。

  分析:对于每一种的硬币可以用k个,如果硬币的价值最小(等于1),最多可以用k个,那就说明对于每一个物品在这里都是刚好可以放满体积为k的背包或者有剩余(或者说溢出,总之就是数量是过量的意思),这样我们就可以将它看成是一个完全背包了。

  状态转移方程:dp[i][j]=(dp[i][j]%MOD+dp[i-1][j-l*a[i]]%MOD)%MOD

  优化以后就是dp[i]=(dp[i]%MOD+dp[i-[i]]%MOD)%MOD  (注意这里的i和上面的i意思不一样,上面的i代表第i中硬币,这里的i代表背包体积)。

上代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #define MAX 10002
 4 #define MOD 100000007
 5 using namespace std;
 6 
 7 int a[102];
 8 int dp[MAX];
 9 
10 
11 int main()
12 {
13      int t,n,k;
14      //freopen("data.txt","r",stdin);
15      scanf("%d",&t);
16      for(int cal=1;cal<=t;cal++){
17         memset(dp,0,sizeof(dp));
18         scanf("%d %d",&n,&k);
19         for(int i=1;i<=n;i++){
20             scanf("%d",&a[i]);
21         }
22         dp[0]=1;
23         for(int i=1;i<=n;i++){
24             for(int j=a[i];j<=k;j++){
25                 dp[j]=(dp[j]+dp[j-a[i]])%MOD;
26             }
27         }
28         printf("Case %d: %d
",cal,dp[k]);
29      }
30     return 0;
31 }
1232
原文地址:https://www.cnblogs.com/sineatos/p/3581431.html