POJ - 1742 Coins (多重dp + biset)

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的数的种类

思路:

 可以用多重dp或者bitset来写,

多重dp,对于每一个数a[i]如果在a[i] - m之间存在数 t 未被标记,减掉k个a[i](k*a[i] <= t)后得到的数 已被标记,那么把 t 标记,在总个数上+1

//
//  main.cpp
//  Coins
//
//  Created by dhl on 2018/7/16.
//  Copyright © 2018年 dhl. All rights reserved.
//

#include <iostream>
#include <string.h>
#include <bitset>
#include <vector>
#include <algorithm>
using namespace std;
int a[100005], b[1005];
int dp[100005], num[100005];
int n, m;

int main(int argc, const char * argv[]) {
    ios::sync_with_stdio(false);
    while(cin >> n >> m && n && m){
        for (int i = 0; i < n; i ++) {
            cin >> a[i];
        }
        for (int i = 0; i < n; i ++) {
            cin >> b[i];
        }
        memset(dp, 0, sizeof(dp));//初始化所有都未标记
        dp[0] = 1;//0被标记
        int ans = 0;
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j <= m; j ++) num[j] = 0;//记录用掉了几个a[i]
            for (int j = a[i]; j <= m; j ++) {
                if (!dp[j] && dp[j - a[i]] && num[j - a[i]] < b[i]) {//如果j未被标记,并且j- a[i]被标记 并且由j到j - a[i] 上用掉的a[i] 小于b[i]
                    num[j] = num[j - a[i]] + 1;//那么j的得到就用掉了bum[j - a[i]] + 1个a[i]
                    dp[j] = 1;//标记
                    ans ++;
                }
            }
        }
        cout << ans << endl;
    }
    
    return 0;
}

bitsest:

bitset就很简单了,只需要优化一下时间,就可以了,优化用二进制优化

#include <iostream>
#include <string.h>
#include <bitset>
#include <vector>
using namespace std;
int a[100005], b[1005];
bitset<100005> aa, bb;
int main(int argc, const char * argv[]) {
    int n, m;
    ios::sync_with_stdio(false);
    while(cin >> n >> m, n, m){
        for (int i = 0; i < n; i ++) {
            cin >> a[i];
        }
        for (int i = 0; i < n; i ++) {
            cin >> b[i];
        }
        aa.reset();
        aa.set(0);
        for (int i = 0; i < n; i ++) {
            for (int j = 1; j < b[i]; j <<= 1) {
                aa |= aa << (a[i] * j);
                b[i] -= j;
            }
            aa |= aa << (a[i] * b[i]);
        }
        int ans = 0;
        for (int i = 1; i <= m; i ++) {
            if (aa[i] == 1)
                ans ++;
        }
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/henu_jizhideqingwa/article/details/81090663