Hdu 6092 Rikka with Subset【背包Dp】

来源:互联网 发布:两少一宽废除知乎 编辑:程序博客网 时间:2024/05/16 23:59

Rikka with Subset

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


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

题目大意:


给你2^n子序列的和的分步情况【0,m】,Bi表示和为i的子序列的个数为Bi个。

让你找到一个原序列,以最小字典序输出。


思路:


我们贪心的去想,一开始的答案序列为空,如果B1>0,那么显然我们想要在结果序列中加一个1,直到当我们的答案序列中的Dp1==B1的时候,我们考虑找下一个位子,如果B2!=Dp2,那么显然我们希望在结果序列中加一个2,依次类推,我们特判一下0,然后加数的过程中拿一个背包去跑即可。


队长Ac代码(偷懒懒得再写一遍了):


#include <bits/stdc++.h>typedef long long int LL;using namespace std;const int N = 1e6+7;const int MOD = 1e9+7;const int INF = 1e9+7;inline int read(){    int x=0,f=1;char c=getchar();    for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1;    for(;'0'<=c&&c<='9';c=getchar())x=(x<<3)+(x<<1)+c-'0';    return x*f;}/********************************************/LL n,m;LL a[333],b[111111];LL h[111111];LL dp[111111];int main(){    int _ = 1,kcase = 0;    for(scanf("%d",&_);_--;){        memset(dp,0,sizeof(dp));        scanf("%lld%lld",&n,&m);        for(int i=0;i<=m;i++) scanf("%lld",&b[i]);        for(int i=1;i<=m;i++) b[i]/=b[0];int l = 0;        b[0]>>=1;        while(b[0]){            a[++l]=0;            b[0]>>=1;        }        dp[0]=1;        for(int i=0;i<=m&&l<n;i++){            while(dp[i]<b[i]&&l<n){                a[++l]=i;                for(int j=m;j>=i;j--)                    dp[j]+=dp[j-i];            }        }        for(int i=1;i<=l;i++) printf("%lld%c",a[i],(i==l)?'\n':' ');    }    return 0;}