2017 HDU 6092 多校联合赛 Rikka with Subset

来源:互联网 发布:软件测试 网盘 编辑:程序博客网 时间:2024/06/05 11:09

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 nn positive A1−An 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

i s he got as Bi.

Yuta shows Rikka the array Bi and he wants Rikka to restore A1−An

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

Input

The first line contains a number t(1≤t≤70), the number of the testcases.

For each testcase, the first line contains two numbers n,m(1≤n≤50,1≤m≤10e4)

The second line contains m+1 numbers B0−Bm(0≤Bi≤2n)

Output

For each testcase, print a single line with n numbers A1−An.

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]

题意:

有两个数组a[ ]和b[ ],给出两个数n和m,表示a[ ]中有 n 个数,所有数之和为 m ,然后又给出了b[ ]数组,

b[ x ]数组中装的是a[ ]中和为 x 的子集个数。例如:a[ ]数组为[ 1,2 ];那么它的子集有[ ],[ 1 ],[ 2 ],

[ 1,2 ];这些子集的和分别为 0,1,2,3;所以b[ 0 ]=1,b[ 1 ]=1,b[ 2 ]=1,b[ 3 ]=1;现在给你b[

]数组,让你求a[ ]数组。

思路:

一个很好的DP题目,以前做过一个用零钱凑总共钱数的问题,这个题目反向,由总钱数求零钱。dp[ x ]中

装的是由小数加和而成的 x 有多少个,然后b[ x ]-dp[ x ]就是真正有多少个 x,然后在再用求出的 x 个数与

前面的小数继续组合求和,不断更新dp[ ]。

代码:

#include<cstdio>#include<cstring>using namespace std;const int Max = 10e4+10;int dp[Max];int a[55];int b[Max];int num[Max];int t,n,m;void work(){    dp[0]=1;    int ant=1;    for(int i=1;i<=m;i++)    {        num[i]=b[i]-dp[i];        for(int u=1;u<=num[i];u++)        {            a[ant++]=i;            for(int j=m;j>=i;j--)            {                dp[j]=dp[j]+dp[j-i];            }        }    }}int main(){    scanf("%d",&t);    while(t--)    {        memset(dp,0,sizeof(dp));        memset(a,0,sizeof(a));        memset(b,0,sizeof(b));        memset(num,0,sizeof(num));        scanf("%d%d",&n,&m);        for(int i=0;i<m+1;i++)        {            scanf("%d",&b[i]);        }        work();        for(int i=1;i<=n;i++)        {            printf("%d%c",a[i],i==n?'\n':' ');        }    }    return 0;}