HDU6092 Rikka with Subset[母函数]

Rikka with Subset

HDU - 6092

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int N=1e4+10;
int B[N];
int c1[N],c2[N],c3[N];
int h1[N],h2[N],h3[N];
int ans[N];
int l1,l2,l3;
int n,m;
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (int i=0 ; i<=m ; ++i)
scanf("%d",&B[i]);
int cnt=0;
if (B[0]>1)
for (int i=0 ; i<B[0]-1 ; ++i)
ans[cnt++]=0;
for (int i=0 ; i<=m ; ++i)
B[i]/=B[0];
int fi=1;
for (int i=1 ; i<=m ; ++i)
{
if (B[i])
{
fi=i;
break;
}
}
for (int i=0 ; i<B[fi]-B[0]+1 ; ++i)
ans[cnt++]=fi;
if (cnt>=n)
{
for (int i=0 ; i<n ; ++i)
{
if (i)
printf(" ");
printf("%d",ans[i]);
}
printf("\n");
continue;
}
for (int i=0 ; i<=m ; ++i)
c1[i]=c2[i]=c3[i]=0;
l1=0;
int now=1;
int up=B[fi],down=1;
for (int i=0 ; i<=B[fi] ; ++i)
{
c1[h1[l1++]=i*fi]=now;
now*=up;
now/=down;
up-=1;
down+=1;
}
for (int i=fi+1 ; i<=m ; ++i)
{
if (B[i] && c1[i]<B[i])
{
for (int j=0 ; j<B[i]-c1[i] ; ++j)
ans[cnt++]=i;
if (cnt>=n)
break;
now=1;
up=B[i]-c1[i];
down=1;
l3=0;
l2=0;
for (int j=0 ; j<=B[i]-c1[i] ; ++j)
{
c3[h3[l3++]=j*i]=now;
now*=up;
now/=down;
up-=1;
down+=1;
}
for (int j=0 ; j<l1 ; ++j)
{
for (int k=0 ; k<l3 ; ++k)
{
if (!c2[h1[j]+h3[k]])
h2[l2++]=h1[j]+h3[k];
c2[h1[j]+h3[k]]+=c1[h1[j]]*c3[h3[k]];
}
}
for (int j=0 ; j<l2 ; ++j)
{
c1[h2[j]]=c2[h2[j]];
h1[j]=h2[j];
c2[h2[j]]=c3[h3[j]]=0;
}
l1=l2;
}
}
for (int i=0 ; i<n ; ++i)
{
if (i)
printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
return 0;
}