F - Coins
Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u
Submit Status

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


多从背包问题,一开始没用多重背包,而是用遍历和hash结合写了一个,那么自然是超时了
当然了,发现自己没有很好的理解了动态规划。
动态规划主要用于可以将问题变为一系列的子问题的最优化处理,另一方面它其实是实现一个记录的作用,这样就不必去重新求解以前求过的问题,而直接用就行了。那么当然就更快乐。
(一超时代码)#include<iostream>
using namespace std;
short int a[100005];//hash数组,用来判断这个硬币数是否存在了
int b[100005];//用来记录已有的coin值的和
int coin[100];
int num[100];
int main()
{
    int n,m;
    int zmax;//当前的最大硬币和
    int endnum;//结果个数
    while(cin>>n>>m)
    {
        if(0==n && 0==m)
            break;
        zmax=0;
        endnum=0;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(int i=0;i<n;i++)
            cin>>coin[i];
        for(int i=0;i<n;i++)
            cin>>num[i];
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<num[i];j++)t6

            {
                for(int z=endnum;z>=0;z--)
                {
                    int tem=coin[i]+b[z];
                    if(tem<=m && 0==a[tem])//
                    {
                        endnum++;
                        b[endnum]=tem;
                        a[tem]=1;
                    }
                }
            }
        }
        cout<<endnum<<endl;
    }
}