hdoj Jam's balance 5616 (母函数&暴力)

211 篇文章 1 订阅
25 篇文章 0 订阅

Jam's balance

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 690    Accepted Submission(s): 328


Problem Description
Jim has a balance and N weights.   (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(1T5) , means T test cases.
For each test case :
The first line is   N , means the number of weights.
The second line are   N  number, i'th number   wi(1wi100)  means the i'th weight's weight is   wi .
The third line is a number   M .   M  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
问题描述
Jam有NN个砝码和一个没有游标的天平,现在给他(1 \leq N \leq 20)(1N20)个砝码,砝码可以放左边,也可以放右边,问可不可以测出所问的重量, 问的个数为(1 \leq M \leq 100)(1M100)个.
输入描述
第一行T(1 \leq T \leq 5)T(1T5),表示TT组数据。
接下来TT组数据:
接下来第一行一个数NN,表示砝码个数。
接下来第二行NN个数,表示砝码们的重量(1 \leq w_i \leq 100)(1wi100)。
接下来第三行一个数MM,表示询问个数。
接下来MM行每行一个数kk,表示一个询问。
输出描述
对于每组数据,输出"YES"或者"NO"
输入样例
1
2
1 4
3
2
4
5
输出样例
NO
YES
YES
Hint
单独放4,可以测出重量为4的
在同一边放4,1,可以测出重量为5的
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
#define N 2010
using namespace std;
int a[21];
int c1[N];
int c2[N];
int main()
{
	int t,n,m,i,j,k;
	scanf("%d",&t);
	while(t--)
	{
		int sum=0;
		scanf("%d",&n);		
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
			sum+=a[i];
		}		
		memset(c1,0,sizeof(c1));
		memset(c2,0,sizeof(c2));
		c1[a[0]]=c1[0]=1;
		for(i=2;i<=n;i++)
		{
			for(j=0;j<=sum;j++)
			{
				for(k=0;k<=a[i-1]&&k+j<=sum;k+=a[i-1])
				{
					c2[k+j]+=c1[j];//相加 
					c2[abs(k-j)]+=c1[j];//相减 
				}
			}
			for(j=0;j<=sum;j++)
			{
				c1[j]=c2[j];
				c2[j]=0;
			}
		}
		scanf("%d",&m);
		while(m--)
		{
			int kk;
			scanf("%d",&kk);
			printf(c1[kk]?"YES\n":"NO\n");
		}
	}
	return 0;
}
//暴力
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
#define ll long long
#define N 2010
using namespace std;
int vis[N];
int s[N];
int main()
{
	int t,n,m,i,j,k;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		memset(vis,0,sizeof(vis));
		memset(s,0,sizeof(s));
		int mm=0;
		vis[0]=0;
		for(i=0;i<n;i++)
		{
			int x;
			scanf("%d",&x);
			s[x]=1;
			for(j=0;j<=mm;j++)
			{
				if(vis[j])
				{
					s[j+x]=1;
					s[abs(j-x)]=1;
				}
			}
			mm+=x;
			for(j=0;j<=mm;j++)
				vis[j]=s[j];
		}
		scanf("%d",&m);
		while(m--)
		{
			scanf("%d",&k);
			printf(vis[k]?"YES\n":"NO\n");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值