HDU 6092 01背包变形 Rikka with Subset

HDU 6092  01背包变形
Rikka with Subset

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1846    Accepted Submission(s): 896


Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Yuta has n.

It is too difficult for Rikka. Can you help her?  
 
Input
The first line contains a number ).
 
Output
For each testcase, print a single line with n.

It is guaranteed that there exists at least one solution. And if there are different solutions, print the lexicographic minimum one.
 
Sample Input
2 2 3 1 1 1 1 3 3 1 3 3 1
 
Sample Output
1 2 1 1 1
Hint

In the first sample, A is [1,2]. A has four subsets [],[1],[2],[1,2] and the sums of each subset are 0,1,2,3. So B=[1,1,1,1]

 
Source
思路:从小到大枚举加入的i值,如果当前的数字组合得到的i的数量小于b[i]那么就要加入对应个i值,同时更新f[i](数字和为i的集合个数)的值,直到填满n个数字。
代码:
 1 #include<bits/stdc++.h>
 2 #define db double
 3 #define ll long long
 4 #define ci(x) scanf("%d",&x)
 5 #define cd(x) scanf("%lf",&x)
 6 #define cl(x) scanf("%lld",&x)
 7 #define pi(x) printf("%d
",x)
 8 #define pd(x) printf("%f
",x)
 9 #define pl(x) printf("%lld
",x)
10 #define fr(i,a,b) for(int i=a;i<=b;i++)
11 using namespace std;
12 const int N=1e5+5;
13 const int mod=1e9+7;
14 const int MOD=mod-1;
15 const db  eps=1e-10;
16 const int inf = 0x3f3f3f3f;
17 int b[N],f[N],a[N];
18 int main()
19 {
20 //    ios::sync_with_stdio(false);
21 //    cin.tie(0);
22     int t;
23     ci(t);
24     for(int ii=1;ii<=t;ii++)
25     {
26         int n,m,c=0;
27         ci(n),ci(m);
28         for(int i=0;i<=m;i++) ci(b[i]);
29         memset(f,0,sizeof(f));
30         f[0]=1;
31         for(int i=1;i<=m;i++){//我们要加入的数字i
32             int v=b[i]-f[i];//加入v个i
33             for(int j=0;j<v;j++){
34                 a[++c]=i;
35                 for(int k=m;k>=i;k--){
36                     f[k]+=f[k-i];//更新当前组合的种数
37                 }
38             }
39         }
40         for(int i=1;i<=n;i++){
41             printf("%d%c",a[i],i==n?'
':' ');
42         }
43     }
44 }