HDU 6092 Rikka with Subset

Rikka with Subset

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


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
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
/*
* @Author: Lyucheng
* @Date:   2017-08-08 13:14:46
* @Last Modified by:   Lyucheng
* @Last Modified time: 2017-08-09 09:34:10
*/
/*
 题意:有一个序列A,给你A的所有子序列的和(2^n)个,每个和出现的次数,让你构造出字典序最小的A

 思路:枚举b[i],每枚举到一个b[i]减去用已知的数构成的i,就是i在序列中有几个,用已知的数构造i,这
    个地方用背包来处理
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define LL long long 
#define MAXN 10005
#define MAXA 55
using namespace std;

int t;
int n,m;
int b[MAXN];
int dp[MAXN];//dp[i]表示用一已知的数字能组成多少种i
int a[MAXA];
int pos=0;

void init(){
    memset(dp,0,sizeof dp);
    pos=0;
}

int main(){
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    scanf("%d",&t);
    while(t--){
        init();
        scanf("%d%d",&n,&m);
        for(int i=0;i<=m;i++){
            scanf("%d",&b[i]);
        }
        dp[0]=1;
        for(int i=1;i<=m;i++){
            int cnt=b[i]-dp[i];
            for(int j=0;j<cnt;j++){
                a[pos++]=i;
                for(int k=m;k>=i;k--){
                    dp[k]+=dp[k-i];
                }
            }
        }
        for(int i=0;i<n;i++){
            printf(i?" %d":"%d",a[i]);
        }
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/7323536.html