跳过正文
  1. Posts/

hdu 1709 The Balance (母函数)

·1 分钟

http://acm.hdu.edu.cn/showproblem.php?pid=1709 题意:有n个砝码,第i个的重量为w[i],问从1到sum(所有砝码的重量之和)那些重量无法称量。(所有质量都是整数) 思路:母函数。 一个砝码可以看做有三种状态,放,放左边(+),放右边(-)

需要注意的一点是放在右边(-)的时候,可能是j-w[i],也可能是w[i]-j

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年02月26日 星期五 17时57分05秒
 4File Name :code/hdu/1709.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=1E4+7;
34int a[N],tmp[N];
35int n;
36int w[N];
37int ans[N];
38int main()
39{
40	#ifndef  ONLINE_JUDGE
41	freopen("code/in.txt","r",stdin);
42  #endif
43
44	while (~scanf("%d",&n))
45	{
46	    for ( int i = 1 ; i <= n ; i++) scanf("%d",&w[i]);
47	  //  sort(w+1,w+n+1);
48	    ms(ans,0);
49	    ms(tmp,0);
50	    ms(a,0);
51	    a[0] = 1;
52	    a[w[1]] = 1;
53
54	    int cur = w[1];
55	    for ( int i = 2 ; i <= n ; i++)
56	    {
57		for ( int j = 0 ; j <= cur ; j++)
58		{
59		    tmp[j] +=a[j];
60		    tmp[j+w[i]]+=a[j];
61		    if (j-w[i]>0) tmp[j-w[i]] +=a[j];
62		    if (w[i]-j>0) tmp[w[i]-j] +=a[j];  //一开始忘记考虑,wa了好多次
63
64		}
65
66		cur += w[i];
67
68		for ( int  j = 0 ; j <= cur ; j++)
69		{
70		   // cout<<"j:"<<j<<" tmp[j]:"<<tmp[j]<<endl;
71		    a[j] = tmp[j];
72		    tmp[j] = 0 ;
73		}
74
75	    }
76	    int cnt = 0 ;
77	    for ( int i = 1 ; i <= cur ; i++)
78	    {
79		if (a[i]==0) ans[++cnt] =  i;
80	    }
81
82	    printf("%d\n",cnt);
83	    if (cnt==0) continue;
84	    for ( int i = 1 ; i <= cnt-1 ; i++) printf("%d ",ans[i]);
85
86	    printf("%d\n",ans[cnt]);
87	}
88
89  #ifndef ONLINE_JUDGE
90  fclose(stdin);
91  #endif
92    return 0;
93}

相关文章

hdu 1085 Holding Bin-Laden Captive! (母函数)

·2 分钟
http://acm.hdu.edu.cn/showproblem.php?pid=1085 题意;一元的钱有num_1张,2元的钱有num_2张,5元的钱有num_5张,问最小的不能组成的钱是多少。 思路:有限个个数的母函数,并且不知道最好要多少,所以限制条件变成了不同种类钱的个数。统计0到num_1+2num_2+5num_5的方案数,第一个为0的就是答案。

hdu 1171 Big Event in HDU (母函数,01背包)

·3 分钟
**Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26534 Accepted Submission(s): 9332 ** Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don’t know that Computer College had ever been split into Computer College and Software College in 2002. The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).