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. Topcoder SRM 630 (500 floyed 暴力 _builtin_popcount())

    题意:给n个点,保证图联通,给点相连的距离,求一个最多的点,这些点之间的距离都是相同的. 分析: 下面的代码是我们房间第一的大神的,写的很简洁,我的思路和他的一样,但是我不知道错哪了. 思路是暴力枚举 ...

  2. 一个简单的iBatis入门例子

    一个简单的iBatis入门例子,用ORACLE和Java测试 目录结构: 1.导入iBatis和oracle驱动. 2.创建类Person.java package com.ibeats;import ...

  3. 在fmri研究中,cca的应用历史

    1.02年ola是第一个应用cca在fmri激活检测上的学者. <exploratory fmri analysis by autocorrelation maximization> 2. ...

  4. 图文详解YUV420数据格式

    YUV格式有两大类:planar和packed.对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V.对于packed的YUV格式,每个像素点的Y,U ...

  5. CImage 获取图片RGB 、图片高和宽;

    1 CImage img , img1 ,imDest; 2 img1.Load( 图片路径); 3 img.Load( 图片路径); 4 为了防止图片失真,先处理一下在把图片显示出来 5 SetSt ...

  6. Linux Shell 脚本

    1. 写一个脚本,利用循环计算10的阶乘#!/bin/shfactorial=1for a in `seq 1 10`do       factorial=`expr $factorial \* $a ...

  7. MSSQL 2005数据库与SP4补丁安装

    Sql Server 2005 正确安装之前的win7配置: http://wenku.baidu.com/link?url=6T3jzVnu2XY_sfqfe9ZqQ_6dUOdrZwHc83baW ...

  8. 关于KOBE 退役

    今天在网上看到一行话,写在KOBE 退役之际 关于职业,最值得问的是自己我有没有像科比那样始终表现出对团队的忠诚和对职业的热爱?从不被别人的误解和攻击打倒?在团队最困难的时候站出来做能做的一切?用职业 ...

  9. HDU 5679 Substring 后缀数组判重

    题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...

  10. IOS 类别与扩展的区别 (category & extensions)

    类别 .h @interface NSString(XXXXXX) -(NSInteger)getLen; @end .m @implementation NSString(XXXXXX) -(NSI ...