Description
Jim has a balance and N weights.
![](https://i-blog.csdnimg.cn/blog_migrate/7df305c950c9fa1d7448a36af4049dc3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a22e580c04f29f0f25e4262eec591fd5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6392eb09bd980819d2aae2f522e4af00.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/48fb42b8e72338f077d46e9ff74ffd93.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1b08ce66336b4f2b3a7b911c59c311d2.png)
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.
![](https://i-blog.csdnimg.cn/blog_migrate/7df305c950c9fa1d7448a36af4049dc3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a22e580c04f29f0f25e4262eec591fd5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6392eb09bd980819d2aae2f522e4af00.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/48fb42b8e72338f077d46e9ff74ffd93.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1b08ce66336b4f2b3a7b911c59c311d2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9f61591de1c8a8c5f8c7d228814838c5.png)
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
![](https://i-blog.csdnimg.cn/blog_migrate/1f80243d0ee60179b3bfaa08e2d41700.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7df305c950c9fa1d7448a36af4049dc3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a22e580c04f29f0f25e4262eec591fd5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1f80243d0ee60179b3bfaa08e2d41700.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b503db96fe85a552479ddbd4b574df16.png)
, means T test cases.
For each test case :
The first line is
, means the number of weights.
The second line are
number, i'th number
![](https://i-blog.csdnimg.cn/blog_migrate/766c416a3ae4d7b5555398bc89a9033a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b6a0880b16005c45f1a99e77c70ee111.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7df305c950c9fa1d7448a36af4049dc3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a22e580c04f29f0f25e4262eec591fd5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/766c416a3ae4d7b5555398bc89a9033a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b6a0880b16005c45f1a99e77c70ee111.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a22e580c04f29f0f25e4262eec591fd5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1b08ce66336b4f2b3a7b911c59c311d2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1b08ce66336b4f2b3a7b911c59c311d2.png)
means the i'th weight's weight is
![](https://i-blog.csdnimg.cn/blog_migrate/766c416a3ae4d7b5555398bc89a9033a.png)
.
The third line is a number
.
is the weight of the object being measured.
![](https://i-blog.csdnimg.cn/blog_migrate/1f80243d0ee60179b3bfaa08e2d41700.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7df305c950c9fa1d7448a36af4049dc3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a22e580c04f29f0f25e4262eec591fd5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1f80243d0ee60179b3bfaa08e2d41700.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b503db96fe85a552479ddbd4b574df16.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9f61591de1c8a8c5f8c7d228814838c5.png)
For each test case :
The first line is
![](https://i-blog.csdnimg.cn/blog_migrate/6392eb09bd980819d2aae2f522e4af00.png)
The second line are
![](https://i-blog.csdnimg.cn/blog_migrate/6392eb09bd980819d2aae2f522e4af00.png)
![](https://i-blog.csdnimg.cn/blog_migrate/766c416a3ae4d7b5555398bc89a9033a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b6a0880b16005c45f1a99e77c70ee111.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7df305c950c9fa1d7448a36af4049dc3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a22e580c04f29f0f25e4262eec591fd5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/766c416a3ae4d7b5555398bc89a9033a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b6a0880b16005c45f1a99e77c70ee111.png)
![](https://i-blog.csdnimg.cn/blog_migrate/d11c95e88ab4c8b84819401a4bd9aa72.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a22e580c04f29f0f25e4262eec591fd5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1b08ce66336b4f2b3a7b911c59c311d2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1b08ce66336b4f2b3a7b911c59c311d2.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9f61591de1c8a8c5f8c7d228814838c5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/766c416a3ae4d7b5555398bc89a9033a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/b6a0880b16005c45f1a99e77c70ee111.png)
The third line is a number
![](https://i-blog.csdnimg.cn/blog_migrate/c6ae951ce5c0f80ccdb9e94033c44e0f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/c6ae951ce5c0f80ccdb9e94033c44e0f.png)
Output
You should output the "YES"or"NO".
Sample Input
1 2 1 4 3 2 4 5
Sample Output
NO YES YES
题目大意:有一个只能判断两边是否相等的天平,现在给你一些已知重量的砝码,问你是否可以通过这些砝码测量一个任意给定的重量(input),即可以通过在天平两侧放一定数量的砝码,使天平平衡。
解题思路:本题需要通过计算出这些所给定的砝码,通过加减运算得到的所有的值,考虑到砝码的数量比较小,可以采用暴力的方法,每一个砝码都有三种状态,添加在左边,添加在右边,不使用。可以很容易的得到递归方程。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int w[25];
int dp[25][3005];//dp[i][j]表示考虑前i个砝码能否构造出重量j,如果能则值为1,不能值为0
int n;
int main()
{
int t,m,i,j,test;
cin>>t;
while(t--)
{
int sum = 0;
memset(dp,0,sizeof(dp));
cin>>n;
for(i=1;i<=n;i++)
{
cin>>w[i];
sum += w[i];
}
dp[0][1000] = 1;//这一步很关键,把所有的重量都加了1000,这样可以避免一些负数被过滤掉的因素
//例如 2 6 7 可以避免-2被过滤掉
// - + +
for(i=1;i<=n;i++)
{
for(j=0;j<=sum+1000;j++)
{
if(dp[i-1][j])
{
dp[i][j] = 1; //表示不使用第i个砝码
if(j+w[i]<=sum+1000)//在已有总和基础上加上第i个砝码
dp[i][j+w[i]] = 1;
if(j-w[i]>=0) //在已有总和的基础上减去第i个砝码
dp[i][j-w[i]] = 1;
}
}
}
cin>>m;
while(m--)
{
cin>>test;
if(dp[n][test+1000] && test>=0 && test<=sum)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
//cout << "Hello world!" << endl;
return 0;
}