题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1709
题意:给你一些砝码,让你输出1—sum中不能称出的重量
题解:直接上母函数,在合并括号的时候有加有减,期中mu()为我自己写的模版,所以有点冗杂
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 const int maxn = 10010; 4 int abs(int a){return a>0?a:-a;} 5 int c1[maxn], c2[maxn],sum;//c1保存每个指数的系数,c2为中间变量 6 int data[110],datanum[110],allnum,ans[5000];//硬币类型从0开始,每种类型数量,类型总数 7 void mu(int n) 8 { 9 for(int i=0;i<=n;i++)c1[i]=0,c2[i]=0; 10 for (int i = 0; i*data[0] <= n&&i<=datanum[0]; i++)c1[i*data[0]] = 1;//c1为第一个括号的系数 11 for (int i = 1; i <allnum; i++){ 12 for (int j = 0; j <= n; j++) 13 for (int k = 0; k + j <= n&&k<=datanum[i]*data[i]; k += data[i])c2[j + k] += c1[j],c2[abs(j-k)]+=c1[j];//关键在这里 14 for (int j = 0; j <= n; j++)c1[j] = c2[j], c2[j] = 0; 15 } 16 } 17 int main(){ 18 int t,i; 19 for(int ii=0;ii<=105;ii++)datanum[ii]=1; 20 while(~scanf("%d",&t)){ 21 for(i=sum=0,allnum=t;i<t;i++)scanf("%d",&data[i]),sum+=data[i]; 22 mu(sum); 23 int cnt=0; 24 for(i=1;i<=sum;i++)if(c1[i]==0)ans[++cnt]=i; 25 printf("%d ",cnt); 26 for(int ik=1;ik<=cnt;ik++)printf("%d%c",ans[ik],ik==cnt?' ':' '); 27 } 28 return 0; 29 }