您的位置:首页 > 其它

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

2016-02-29 08:32 387 查看

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


[align=left]Problem Description[/align]
Jim has a balance and N weights. (1≤N≤20)

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.
 

[align=left]Input[/align]
The first line is a integer T(1≤T≤5),
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(1≤wi≤100) 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.
 

[align=left]Output[/align]
You should output the "YES"or"NO".
 

[align=left]Sample Input[/align]

1
2
1 4
3
2
4
5

 

[align=left]Sample Output[/align]

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)(1≤N≤20)个砝码,砝码可以放左边,也可以放右边,问可不可以测出所问的重量, 问的个数为(1 \leq M \leq 100)(1≤M≤100)个.
输入描述
第一行T(1 \leq T \leq 5)T(1≤T≤5),表示TT组数据。
接下来TT组数据:
接下来第一行一个数NN,表示砝码个数。
接下来第二行NN个数,表示砝码们的重量(1 \leq w_i \leq 100)(1≤w​i​​≤100)。
接下来第三行一个数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
;
int c2
;
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
;
int s
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: