hdu 1709 The Balance
母函数的特殊情况,左右两边都可以放,如样例1,2,9
母函数为(1+x+1/x)*(1+x^2+1/x^2)*(1+x^9+1/x^9)
化简为(1+x+x^2)*(1+x^2+x^4)*(1+x^9+x^18)/(x*x^2*x^9)
这样就好计算了,看代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#include<stdlib.h>
using namespace std;
__int64 a[30001],b[30001],an[10001];
int main()
{
int i,j,k,n,sum,num;
while (cin>>n&&n){
sum=0;num=0;
for(i=0;i<n;i++){
cin>>an[i];
sum+=an[i];
}
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i=0;i<=2*an[0];i+=an[0])
a[i]=1;
for(i=1;i<n;i++){
for(j=0;j<=sum;j++)
for(k=0;k<=sum&&k<=2*an[i];k+=an[i]){
b[j+k]+=a[j];
}
for(j=0;j<=sum;j++){
a[j]=b[j];
b[j]=0;
}
}
for(i=sum,j=0;i>=0;i--)
if(a[i]==0){
num++;
an[j++]=sum-i;
}
if(num==0)
{
cout<<num<<endl;
continue;
}
cout<<num<<endl<<an[0];
for(i=1;i<num;i++)
cout<<' '<<an[i];
cout<<endl;
}
return 0;
}