BC #70 B || hdu 5616 Jam's balance (母函数)

http://acm.hdu.edu.cn/showproblem.php?pid=5616 题意:有n个(n<=20)砝码,第i个重量为w[i],给出m个查询,每个查询一个重量,问这个重量能否被称量出。 思路:暴力(没美感),01背包(不会),母函数(瞬间成了傻逼题)和这题很像 hdu1709 balance hdu1709解题报告

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年02月27日 星期六 15时35分32秒
 4File Name :code/hdu/5616.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=2E3+7;
34int w[N];
35int a[N],tmp[N];
36int n;
37int m;
38int main()
39{
40	#ifndef  ONLINE_JUDGE 
41	freopen("code/in.txt","r",stdin);
42  #endif
43
44	ios::sync_with_stdio(false);
45	int T;
46	cin>>T;
47	while (T--)
48	{
49	    cin>>n;
50	    for ( int i = 1 ; i <= n ; i++) cin>>w[i];
51
52	    ms(a,0);
53	    ms(tmp,0);
54	    a[0] = 1;
55	    a[w[1]] = 1;
56
57	    int cur = w[1];
58	    for ( int i = 2 ; i <= n ; i++)
59	    {
60		for ( int j = 0 ; j <= cur ; j++)
61		{
62		    tmp[j+w[i]] += a[j]; //放左边
63		    tmp[j] +=a[j];      //不放
64
65		    if (j-w[i]>0) tmp[j-w[i]] += a[j];  //放右边
66		    if (w[i]-j>0) tmp[w[i]-j] += a[j];
67		}
68
69
70		cur += w[i];
71		for ( int j  = 0 ; j <= cur ; j++)
72		{
73		    a[j] = tmp[j];
74		    tmp[j] =  0;
75		}
76	    }
77
78	    cin>>m;
79	    while (m--)
80	    {
81		int x;
82		cin>>x;
83		if (a[x]==0)
84		{
85		    puts("NO");
86		}
87		else
88		{
89		    puts("YES");
90		}
91	    }
92	}
93
94  #ifndef ONLINE_JUDGE  
95  fclose(stdin);
96  #endif
97    return 0;
98}