Try Again

POJ 1742 Coins 优化后的多重背包

Coins
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 37853   Accepted: 12849

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
  第一行输入,n,m分别表示n种硬币,m表示总钱数。
  第二行输入n个硬币的价值,和n个硬币的数量。
  输出这些硬币能表示的所有在m之内的硬币种数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int dp[100006],pos[100005];
int a[105],b[105];
int k,x,n,m,ans;
int main()
{
    while(scanf("%d%d",&n,&m) && n+m)
    {
        memset(dp,0,sizeof(dp));
        ans=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d",&b[i]);
        }
        dp[0]=1;
        for(int i=0;i<n;i++)
        {
            memset(pos,0,sizeof(pos));
            for(int j=a[i];j<=m;j++)
            {
                if(!dp[j] && dp[j-a[i]] && pos[j-a[i]]<b[i])
                {
                    pos[j]=pos[j-a[i]]+1;
                    dp[j]=1;
                    ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

 

posted @ 2017-07-17 10:13  十年换你一句好久不见  阅读(192)  评论(0编辑  收藏  举报