HDU 5616 Jam's balance

Problem Description

Jim has a balance and N weights. (1 \leq N \leq 20)(1N20) The balance can only tell whether things on different side are the same weight. Weights can be put on left side or right side arbitrarily. Please tell whether the balance can measure an object of weight M.

Input

The first line is a integer T(1 \leq T \leq 5)T(1T5), means T test cases. For each test case : The first line is NN, means the number of weights. The second line are NN number, i'th number w_i (1 \leq w_i \leq 100)wi(1wi100) means the i'th weight's weight is w_iwi. The third line is a number MMMM is the weight of the object being measured.

Output

You should output the "YES"or"NO".

Sample Input
1
2
1 4
3
2
4
5
Sample Output
NO
YES
YES


    
    
    
    
Hint
For the Case 1:Put the 4 weight alone

For the Case 2:Put the 4 weight and 1 weight on both side

简单的dp就可以了,但是需要注意的是问的那个数的大小

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;

int T, n, m, f[21][4005], x;

int main()
{
    scanf("%d", &T);
    while (scanf("%d", &n) != EOF, T--)
    {
        memset(f, 0, sizeof(f));
        f[0][2000] = 1;
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &x);
            for (int j = x; j <= 4000 - x; j++)
            {
                if (f[i][j]) f[i + 1][j - x] = f[i + 1][j + x] = f[i + 1][j] = 1;
            }
        }
        scanf("%d", &m);
        while (m--)
        {
            scanf("%d", &x);
            if (x >= 0 && x <= 2000)
                if (f[n][2000 + x] || f[n][2000 - x]) printf("YES\n");
                else printf("NO\n");
            else printf("NO\n");
        }
    }
    return 0;
}


你可能感兴趣的:(HDU)