poj1742(多重背包分解+01背包二进制优化)

Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.

Input

The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4
 
题目的意思:
  第一行输入,n,m分别表示n种硬币,m表示总钱数。
  第二行输入n个硬币的价值,和n个硬币的数量。
  输出这些硬币能表示的所有在m之内的硬币种数。

思路:此问题可以转换为01背包和完全背包问题求解,而01背包又可以进行二进制的优化
例:13可由1 2 4 6这四个数字可以组成从1到13所有的数字,并且还不会重复使用,1--->1; 2--->2; 3--->1,2; 4--->4; 5--->1,4; 6--->2,4; 7--->1,6; 8--->2,6; 9--->1,2,6; 10--->4,6; 11--->1,4,6; 12--->2,4,6; 13--->1,2,4,6;
 拆分方法:
{
  num;数字
  k=1;
  val;价值
  while(k<=num)
  {
    01beibao(k*val);
    num-=k;
    k=k*2;
  }
  01beibao(num);
}
代码:http://blog.csdn.net/vvmame/article/details/76697198
自己的代码:

#include<stdio.h>
#include<string.h>
struct
{
  int p;
  int nb;
}mp[150];
int dp[100050];//记录该点是否可以有硬币的组成而得到
int lg[100050];//记录每个状态硬币使用次数
int main()
{
  int n,m;
  int i,j,k,ans;
  while(scanf("%d%d",&n,&m)!=EOF&&(m!=0||n!=0))
  {
    ans=0;
    memset(dp,0,sizeof(dp));
    dp[0]=1;
    for(i=1;i<=n;i++)
    scanf("%d",&mp[i].p);
    for(i=1;i<=n;i++)
    scanf("%d",&mp[i].nb);
    for(i=1;i<=n;i++)
    {
      memset(lg,0,sizeof(lg));
      for(j=mp[i].p;j<=m;j++)
      {
        if(!dp[j]&&dp[j-mp[i].p]&&lg[j-mp[i].p]<mp[i].nb)
        {
          dp[j]=1;
          ans++;
          lg[j]=lg[j-mp[i].p]+1;
        }
      }
    }
    printf("%d\n",ans);
  }
return 0;
}

posted @ 2018-03-17 16:46  cglong  阅读(299)  评论(0编辑  收藏  举报