Coins (poj 1742 && hdu 2844 DP)


Language:
Default

Coins

Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 30047   Accepted: 10195

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

Source

field=source&key=LouTiancheng%40POJ" style="text-decoration:none">[email protected]

题意:给n张不同面值的钱,每种面值的钱都有一定数量,问用这些钱可以凑出多少种不同的面值,而且面值要在1~m内。输出种数。

思路:dp[i]表示i面值的钱是否可以凑出来(0或1)。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 100005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

int n,m;
int dp[maxn],num[maxn];
int v[111],c[111];

int main()
{
    int i,j;
    while (sff(n,m)&&(n+m))
    {
        mem(dp,0);
        FRL(i,0,n)
            sf(v[i]);   //v[i]存的面值
        FRL(i,0,n)
            sf(c[i]);   //c[i]存的每一个面值相应的数量
        int ans=0;
        dp[0]=1;
        FRL(i,0,n)
        {
            mem(num,0);  //num[j]表示凑够面值j时用了多少v[i]面值的钱
            FRE(j,v[i],m)
            {
                if (!dp[j]&&dp[j-v[i]]&&num[j-v[i]]<c[i])     //j面值之前没有凑出来过而且j-v[i]面值的凑出来过,这样就能够添加一张v[i]凑成新面值
                {
                    ans++;
                    dp[j]=1;
                    num[j]=num[j-v[i]]+1;
                }
            }
        }
        pf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-05 08:49:09

Coins (poj 1742 &amp;&amp; hdu 2844 DP)的相关文章

Coins (poj 1742 &amp;&amp; hdu 2844 DP)

Language: Default Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 30047   Accepted: 10195 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 fou

hdu 4865 Peter&#39;s Hobby (隐马尔可夫模型 dp)

Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 292    Accepted Submission(s): 132 Problem Description Recently, Peter likes to measure the humidity of leaves. He recorded a leaf

HDU - 4734 F(x) (2013成都网络赛,数位DP)

题意:求0-B的满足<=F[A]的所有可能 思路:数位DP,记忆化搜索 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int A, B; int dp[20][200000]; int bit[20]; int dfs(int cur, int num, int flag) { if (cur == -

hdu 4865 Peter&amp;#39;s Hobby (隐马尔可夫模型 dp)

Peter's Hobby Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 292    Accepted Submission(s): 132 Problem Description Recently, Peter likes to measure the humidity of leaves. He recorded a leaf

UVa 1252 - Twenty Questions(记忆化搜索,状态压缩dp)

题目链接:uva 1252 题意: 有n个长度为m的二进制串,每个都是不同的. 为了把所有字符串区分开,你可以询问,每次可以问某位上是0还是1. 问最少提问次数,可以把所有字符串区分开来. 思路来源于:点击打开链接 思路: m很小,可以考虑状态压缩. dp[s1][s2]表示询问的状态为s1时,此时能猜到状态包含s2时最小需要的步数. 当询问的几位=s2的二进制串小于2时就能区分出来了,dp[s1][s2]=0: 不能区分则再询问一次,s1|=(1<<k),如果问某位为0,则s2不变,问某位为

HDU - 4514 湫湫系列故事——设计风景线(并查集判环+树形DP)

题目链接:https://vjudge.net/problem/HDU-4514 随着杭州西湖的知名度的进一步提升,园林规划专家湫湫希望设计出一条新的经典观光线路,根据老板马小腾的指示,新的风景线最好能建成环形,如果没有条件建成环形,那就建的越长越好.  现在已经勘探确定了n个位置可以用来建设,在它们之间也勘探确定了m条可以设计的路线以及他们的长度.请问是否能够建成环形的风景线?如果不能,风景线最长能够达到多少?  其中,可以兴建的路线均是双向的,他们之间的长度均大于0. Input 测试数据有

Bone Collector II(HDU 2639 DP)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3471    Accepted Submission(s): 1792 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took par

HDU 4336 Card Collector(容斥原理 or 状压求期望dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4336 Problem Description In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, for example, if you collect all the 108 people in the famous novel Water Margin,

UVA 1347(POJ 2677) Tour(双色欧几里德旅行商问题)

Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination is r