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. CocoaPods requires your terminal to be using UTF-8 encoding

    WARNING: CocoaPods requires your terminal to be using UTF-8 encoding. See https://github.com/CocoaPo ...

  2. UVa 1475 (二分+半平面交) Jungle Outpost

    题意: 有n个瞭望塔构成一个凸n边形,敌人会炸毁一些瞭望台,剩下的瞭望台构成新的凸包.在凸多边形内部选择一个点作为总部,使得敌人需要炸毁的瞭望塔最多才能使总部暴露出来.输出敌人需要炸毁的数目. 分析: ...

  3. 完整cocos2d-x编译Andriod应用过程

    作者:何卫 转载请注明,原文链接:http://www.cnblogs.com/hewei2012/p/3366969.html 其他平台移植:http://cocos2d.cocoachina.co ...

  4. memcache的最佳实践方案。

    基本问题 1.memcached的基本设置 1)启动Memcache的服务器端 # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 ...

  5. Oracle 课程九之绑定变量

    课程目标 完成本课程的学习后,您应该能够: •变量绑定的目的 •父子游标 •游标共享 •绑定窥探 •SQL语句处理流程 •硬解析.软解析.软软解析 •变量绑定的应用场景   1.游标 游标可以理解为S ...

  6. Hadoop序列化

      遗留问题: Hadoop序列化可以复用对象,是在哪里复用的? 介绍Hadoop序列化机制 Hadoop序列化机制详解 Hadoop序列化的核心 Hadoop序列化的比较接口 ObjectWrita ...

  7. hdu 3746 Cyclic Nacklace(KMP)

    题意: 求最少需要在后面补几个字符能凑成两个循环. 分析: 最小循环节的应用,i-next[i]为最小循环节. #include <map> #include <set> #i ...

  8. duilib入门之贴图描述、类html文本描述、动态换肤、Dll插件、资源打包

    转载自duilib入门文档 贴图描述: Duilib的表现力丰富很大程度上得益于贴图描述的简单强大.Duilib的贴图描述分为简单模式和复杂模式两种. 简单模式使用文件名做为贴图描述内容,在这种方式下 ...

  9. Myeclipse8.5 svn插件安装两种方式

    第一种方式:(亲测成功)第一步:准备插件包:site-1.6.18.zip解压该包里面有features和plugins文件夹,删除该包里面的xml结尾的文件. 第二:我的Myeclipse8.5安装 ...

  10. ili9341 横屏驱动代码

    void ili9341_Initializtion(void) { u16 i; RCC->APB2ENR|=<<; //使能PORTB时钟 GPIOB->CRH&= ...