zoukankan      html  css  js  c++  java
  • Coins(hdu 2844 多重背包)

    Coins

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 10632    Accepted Submission(s): 4230


    Problem Description
    Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse 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        //n面值种类,m最大价格
    1 2 4 2 1 1      // 3种面值,对应个数,求最多能凑出多少种小于等于m的钱数
    2 5
    1 4 2 1
    0 0
     

    Sample Output
    8
    4
    一开始想到用母函数求解,数据量有点大,看了discuss然后用多重背包+二进制优化,二进制优化不能用pow函数,不然依然超时。。
     1 #include <stdio.h>
     2 #define max(a,b) a>b?a:b
     3 int main()
     4 {
     5     int n;
     6     int a[101],b[101];
     7     int f[100001];
     8     int i,j,m,p,g;
     9     int sum=0;
    10     freopen("in.txt","r",stdin);
    11     int q;
    12     while(scanf("%d%d",&n,&m)!=EOF)
    13     {
    14         sum=0;
    15         for(i=0;i<=m;i++)   f[i]=0;
    16         if(m==0&&n==0)
    17             return 0;
    18         for(i=1;i<=n;i++)
    19             scanf("%d",&a[i]);
    20         for(i=1;i<=n;i++)
    21             scanf("%d",&b[i]);
    22         for(i=1;i<=n;i++)
    23         {   
    24             p=0;
    25             g=1;
    26             while(b[i]>g)
    27             {
    28                 q=a[i]*g;
    29                 for(j=m;j>=q;j--)
    30                 {
    31                     f[j]=max(f[j],f[j-a[i]*g]+a[i]*g);
    32                 }
    33                 b[i]-=g;
    34                 g=g*2;
    35             }
    36             q=a[i]*b[i];
    37             for(j=m;j>=q;--j)
    38                 f[j]=max(f[j],f[j-a[i]*b[i]]+a[i]*b[i]);
    39         }
    40         for(i=1;i<=m;i++)
    41         {
    42             if(f[i]==i)
    43                 sum++;
    44         }
    45         printf("%d
    ",sum);
    46     }
    47 }
    View Code

    母函数超时代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 using namespace std;
     6 int val[100+10],num[100+10];
     7 int c1[100000+1],c2[100000+1];
     8 int m,n;
     9 int solve()
    10 {
    11     int ans=0,u;
    12     int i,j,k,p=num[1]*val[1];
    13     int t=min(val[1]*num[1],m);
    14     for(i=0;i<=t;i+=val[1])
    15         c1[i]=1;
    16     for(i=0;i<=t;i++)
    17         c2[i]=0;
    18     for(i=2;i<=n;i++)
    19     {
    20         for(j=0;j<=p;j++)
    21         {
    22             u=-1;
    23             for(k=0;j+k<=m;k+=val[i])
    24             {
    25                 u++;
    26                 if(u>num[i])
    27                     break;
    28                 c2[j+k]+=c1[j];
    29             }
    30         }
    31         p=j+k;
    32         for(j=0;j<=p;j++)
    33         {
    34             c1[j]=c2[j];
    35             c2[j]=0;
    36         }
    37     }
    38     for(i=1;i<=m;i++)
    39     {
    40         if(c1[i]!=0)
    41             ans++;
    42     }
    43     return ans;
    44 }
    45 int main()
    46 {
    47     int i,j;
    48     freopen("in.txt","r",stdin);
    49     while(scanf("%d%d",&n,&m))
    50     {
    51         if(m==0&&n==0)
    52             return 0;
    53         for(i=1;i<=n;i++)
    54             scanf("%d",&val[i]);
    55         for(i=1;i<=n;i++)
    56             scanf("%d",&num[i]);
    57         cout<<solve()<<endl;
    58     }
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    【算法总结】搜索算法(上)
    New Beginning
    好想退役啊【笑
    【NOIP2012】DAY1+DAY2题解
    【NOIP2013】Day2不完全题解+代码
    【NOIP2013】DAY1题解+代码
    【NOIP2014】DAY2题解+代码
    【游记】NOIP2015造纸记
    【ACM-ICPC 2018 徐州赛区网络预赛】E. End Fantasy VIX 血辣 (矩阵运算的推广)
    【ACM-ICPC 2018 沈阳赛区网络预赛】不太敢自称官方的出题人题解
  • 原文地址:https://www.cnblogs.com/a1225234/p/5056597.html
Copyright © 2011-2022 走看看