码迷,mamicode.com
首页 > 其他好文 > 详细

F - Coins

时间:2014-08-17 22:37:02      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   

F - Coins
Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

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




多重背包,废话不多,看代码就OK了,
#include<iostream>
using namespace std;
int dp[100005];
int sum[100005];
int coin[100],numofcoin[100];
int main()
{
    int n,m;
    int num;
    while(cin>>n>>m)
    {
        if(0==n && 0==m)
            break;
        for(int i=0;i<n;i++)
            cin>>coin[i];
        for(int i=0;i<n;i++)
            cin>>numofcoin[i];
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        num=0;
        for(int i=0;i<n;i++)
        {
            memset(sum,0,sizeof(sum));
            for(int j=coin[i];j<=m;j++)
            {
                if(!dp[j] && dp[j-coin[i]] && sum[j-coin[i]]<numofcoin[i])//
                {
                    num++;
                    dp[j]=1;
                    sum[j]=sum[j-coin[i]]+1;
                }
            }
        }
        cout<<num<<endl;
    }
}
当然,这个还可以化成一维背包来做,但是一维背包会超时,这时可以做一些优化,如15个1可以化成1,,2,4,8,0;
这是我的代码,但是没有AC,目前还未解决
#include<iostream>
#include<math.h>
using namespace std;
int dp[100005];
int coin[100],numofcoin[100];
int numofGroups(int i)
{
    int n=0;
    for(;pow(2.0,n)<numofcoin[i];n++);
    return n+1;
}
int main()
{
    int n,m;
    int num;
    while(cin>>n>>m)
    {
        if(0==n && 0==m)
            break;
        for(int i=0;i<n;i++)
            cin>>coin[i];
        for(int i=0;i<n;i++)
            cin>>numofcoin[i];
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        num=0;
        for(int i=0;i<n;i++)
        {
            int groups=numofGroups(i);
            for(int j=0;j<groups;j++)//一维背包
            {
                int tem=0;
                if(j==groups-1)
                    tem=numofcoin[i]-(int)pow(2.0,j)+1;
                else
                    tem=(int)pow(2.0,j);
                for(int k=m;k>=tem*coin[i];k--)
                {
                    if(!dp[k] && dp[k-tem*coin[i]])
                    {
                        dp[k]=1;
                        num++;
                    }
                }
            }
        }
        cout<<num<<endl;
    }
}

F - Coins,布布扣,bubuko.com

F - Coins

标签:des   style   http   color   os   io   strong   for   

原文地址:http://www.cnblogs.com/zhaoxinshanwei/p/3918314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!