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. C# 创建iis站点以及IIS站点属性,iis不能启动站点

    DontLog = False是否将客户端的请求写入日志文件 2011年04月09日 #region CreateWebsite 新增网站 public string CreateWebSite(st ...

  2. HDU 1863 畅通工程(最小生成树,prim)

    题意: 给出图的边和点数,要求最小生成树的代价,注:有些点之间是不可达的,也就是可能有多个连通图.比如4个点,2条边:1-2,3-4. 思路: 如果不能连通所有的点,就输出‘?’.之前以为每个点只要有 ...

  3. 让你的 Node.js 应用跑得更快的 10 个技巧(转)

    Node.js 受益于它的事件驱动和异步的特征,已经很快了.但是,在现代网络中只是快是不行的.如果你打算用 Node.js 开发你的下一个Web 应用的话,那么你就应该无所不用其极,让你的应用更快,异 ...

  4. 【转】开始iOS 7中自动布局教程(一)

    原文网址:http://www.cocoachina.com/industry/20131203/7462.html 原文:Beginning Auto Layout Tutorial in iOS ...

  5. 写的cursor demo仅作记录

    declare @objectID int; declare objcur cursor for object_id from m_object open objcur fetch next from ...

  6. POJ 1064 Cable master

    Cable master Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37865   Accepted: 8051 Des ...

  7. [Everyday Mathematics]20150125

    试求极限 $$\bex \lim_{x\to 0^+}\int_x^{2x} \frac{\sin^m t}{t^n}\rd t\quad\sex{m,n\in\bbN}. \eex$$

  8. 数据库SQL Server与C#中数据类型的对应关系

    ylbtech- .NET-Basic:数据库SQL Server与C#中数据类型的对应关系 数据库SQL SServer与C#中数据类型的对应关系 1.A,返回顶部 数据库 C#程序 int int ...

  9. 五指CMS 3.0 手动升级方法

  10. ldconfig报错 :libstdc++.so.6.0.18-gdb.py不是一个elf文件

    今天安装wxWidgets,输入ldconfig竟然提示 /usr/lib64/libstdc++.so.6.0.18-gdb.py 不是一个elf文件,开头魔数错误 摸不着头脑,上网搜了一下,有说是 ...