HDU 6092:Rikka with Subset

来源:互联网 发布:lda模型 矩阵分解 编辑:程序博客网 时间:2024/05/21 19:37

Rikka with Subset

                                                                 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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 positive A1An and their sum is m. Then for each subset S of A, Yuta calculates the sum of S

Now, Yuta has got 2n numbers between [0,m]. For each i[0,m], he counts the number of is he got as Bi.

Yuta shows Rikka the array Bi and he wants Rikka to restore A1An.

It is too difficult for Rikka. Can you help her?  
 

Input
The first line contains a number t(1t70), the number of the testcases. 

For each testcase, the first line contains two numbers n,m(1n50,1m104).

The second line contains m+1 numbers B0Bm(0Bi2n).
 

Output
For each testcase, print a single line with n numbers A1An.

It is guaranteed that there exists at least one solution. And if there are different solutions, print the lexicographic minimum one.
 

Sample Input
22 31 1 1 13 31 3 3 1
 

Sample Output
1 21 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
2017 Multi-University Training Contest - Team 5


题意:给一个数列a[1.....n],m是a数列所有元素的和,给出数列b[0.....m],b[i]代表a数列的子集中和为i的子集的个数。现在给出数列b,让你求数列a(有多解的话,字典序要最小,不过好像只有一个解。。。)。
思路:逆向DP。
可以想到b[i]是由a数列中的元素进行DP得到的。
即:
for(int i=1;i<=n;i++)
{
for(int j=m;j>=a[i];j--)b[j]+=b[j-a[i]];
}
那么我们就可以反过来求出数列a;

#include<bits/stdc++.h>using namespace std;const int maxn = 1e4+5;long long b[maxn],a[100];int main(){long long T,n,m;cin>>T;while(T--){cin>>n>>m;for(long long i=0;i<=m;i++)scanf("%lld",&b[i]);for(long long i=1;i<=m;i++){if(b[i]){a[1]=i;break;} //求出a的第一个数(因为最小的i且b[i]!=0一定代表i是a中的最小数)}long long cnt=2;for(long long i=1;i<=n;i++) //从第一个数反过来DP{for(long long j=a[i],tag=0;j<=m;j++){b[j]-=b[j-a[i]];if(b[j]>0&&tag==0)a[cnt++]=j,tag=1; //每次都要把最小的i且b[i]!=0的数加入a中}}for(long long i=1;i<=n;i++)printf("%lld%c",a[i],i==n?'\n':' ');}return 0;}