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解题报告
/* ***********************************************
Author :111qqz
Created Time :2016年02月27日 星期六 15时35分32秒
File Name :code/hdu/5616.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=2E3+7;
7int w[N];
8int a[N],tmp[N];
9int n;
10int m;
11int main()
12{
13 #ifndef ONLINE_JUDGE
14 freopen("code/in.txt","r",stdin);
15 #endif
1 ios::sync_with_stdio(false);
2 int T;
3 cin>>T;
4 while (T--)
5 {
6 cin>>n;
7 for ( int i = 1 ; i <= n ; i++) cin>>w[i];
1 ms(a,0);
2 ms(tmp,0);
3 a[0] = 1;
4 a[w[1]] = 1;
1 int cur = w[1];
2 for ( int i = 2 ; i <= n ; i++)
3 {
4 for ( int j = 0 ; j <= cur ; j++)
5 {
6 tmp[j+w[i]] += a[j]; //放左边
7 tmp[j] +=a[j]; //不放
1 if (j-w[i]>0) tmp[j-w[i]] += a[j]; //放右边
2 if (w[i]-j>0) tmp[w[i]-j] += a[j];
3 }
1 cur += w[i];
2 for ( int j = 0 ; j <= cur ; j++)
3 {
4 a[j] = tmp[j];
5 tmp[j] = 0;
6 }
7 }
1 cin>>m;
2 while (m--)
3 {
4 int x;
5 cin>>x;
6 if (a[x]==0)
7 {
8 puts("NO");
9 }
10 else
11 {
12 puts("YES");
13 }
14 }
15 }
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}