F - Coins

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

Submit Status Practice POJ 1742

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

时间: 2024-11-05 14:53:33

F - Coins的相关文章

Project Euler:Problem 31 Coin sums

In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation: 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). It is possible to make £2 in the following way: 1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

过分过分进货价获国家

http://f.dangdang.com/group/24554/3491082/http://f.dangdang.com/group/24554/3491087/http://f.dangdang.com/group/24554/3491094/http://f.dangdang.com/group/24554/3491099/http://f.dangdang.com/group/24554/3491105/http://f.dangdang.com/group/24554/349111

我们找个地方看好戏

http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m041433ssun.html http://v.qq.com/page/f/y/4/m04143o3lhg.html http://v.qq.com/page/f/y/4/m04144675h3.html http://v.qq.com/page/f/y/4/m04144k1k1j.html http://v.qq.com/page/f/y/4/m04

【动态规划】Gym - 101102A - Coins

Hasan and Bahosain want to buy a new video game, they want to share the expenses. Hasan has a set of N coins and Bahosain has a set of M coins. The video game costs W JDs. Find the number of ways in which they can pay exactly W JDs such that the diff

H - Coins

H - Coins Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One day Hibix opened purse and found there were some coins. He decided to

背包问题 POJ1742 Coins

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 37762   Accepted: 12830 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

Lucky Coins Sequence

Lucky Coins Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 35 Accepted Submission(s): 29   Problem Description As we all know,every coin has two sides,with one side facing up and another

POJ1742 Coins[多重背包可行性]

Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 34814   Accepted: 11828 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