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的更多相关文章

  1. Mysql_以案例为基准之查询

    查询数据操作

  2. 完全背包和多重背包的混合 F - The Fewest Coins

    http://poj.org/problem?id=3260 这个题目有点小难,我开始没什么头绪,感觉很乱. 后来看了题解,感觉豁然开朗. 题目大意:就是这个人去买东西,东西的价格是T,这个人拥有的纸 ...

  3. POJ3260The Fewest Coins[背包]

    The Fewest Coins Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6299   Accepted: 1922 ...

  4. POJ1742 Coins[多重背包可行性]

    Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 Descripti ...

  5. hdu 2844 多重背包coins

    http://acm.hdu.edu.cn/showproblem.php?pid=2844 题意: 有n个硬币,知道其价值A1.....An.数量C1...Cn.问在1到m价值之间,最多能组成多少种 ...

  6. POJ3260——The Fewest Coins(多重背包+完全背包)

    The Fewest Coins DescriptionFarmer John has gone to town to buy some farm supplies. Being a very eff ...

  7. hdu 2844 poj 1742 Coins

    hdu 2844 poj 1742 Coins 题目相同,但是时限不同,原本上面的多重背包我初始化为0,f[0] = 1;用位或进行优化,f[i]=1表示可以兑成i,0表示不能. 在poj上运行时间正 ...

  8. Coins(hdu 2844 多重背包)

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. SGU 415. Necessary Coins ( 背包dp )

    题意大概是:给出N个硬币, 面值为a_i, 问要凑成X元哪些硬币是不可或缺的.1 ≤ N ≤ 200, 1 ≤ x ≤ 10^4 直接枚举, 然后就是01背包了. 为了不让复杂度多乘个N, 我们就从左 ...

随机推荐

  1. Android开发性能优化大总结

    1.  采用硬件加速,在androidmanifest.xml中application添加android:hardwareAccelerated="true".不过这个需要在and ...

  2. RazorEngine 3.6.5.0

    public class Person { public string Name { get; set; } public string Code { get; set; } } var templa ...

  3. iOS开发:Swift多线程NSOperation的使用

    介绍: NSOperation是基于GCD实现,封装了一些更为简单实用的功能,因为GCD的线程生命周期是自动管理,所以NSOperation也是自动管理.NSOperation配合NSOperatio ...

  4. 数据库编程与C#编程互译

    今天有一段代码,先是用程序实现. 闲来无聊,又用存储过程实现了一次. 程序中实现. /// <summary> /// 根据区域和用户名获取可访问的国家 /// </summary& ...

  5. BZOJ2594: [Wc2006]水管局长数据加强版

    题解: 裸LCT+离线+二分+MST... 代码:(几乎摘抄自hzwer) #include<cstdio> #include<cstdlib> #include<cma ...

  6. SpringMVC——注解的使用与结果跳转方式

    1.项目结构 2.源代码 package com.zhengbin.controller; import java.io.IOException; import javax.servlet.Servl ...

  7. 【转】《APUE》第三章笔记(4)及习题3-2

    原文网址:http://www.cnblogs.com/fusae-blog/p/4256794.html APUE第三章的最后面给出的函数,现在还用不着,所以,先留个名字,待到时候用着了再补上好了. ...

  8. 30种oracle常见的等待事件说明

    1Buffer busy waits从本质上讲,这个等待事件的产生仅说明了一个会话在等待一个Buffer(数据块),但是导致这个现象的原因却有很多种.常见的两种是: 当一个会话视图修改一个数据块,但这 ...

  9. OracleHelper(for produce)

    OracleHelper中,有一个用存储过程实现的Insert方法. 然后我把执行存储过程的方法 封装成了可以执行任何存储过程,参数是 存储过程名称 以及存错过程中的传入.传出参数 using Ora ...

  10. YUV采样及存储格式

    YUV,分为三个分量,“Y”表示明亮度(Luminance或Luma),也就是灰度值:而“U”和“V” 表示的则是色度(Chrominance或Chroma),作用是描述影像色彩及饱和度,用于指定像素 ...