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 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 is 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≤104).
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
 
假设现在有一个元素个数为n的允许有重复元素的集合a,那么这个a的子集就有2^n个子集,现在给你这2^n个子集的每一个的和(cnt[i]表示子集的和为i的子集个数),让你还原a集合
第2个样例意思为子集的和为0 1 2 3 的子集个数分别有1 3 3 1个,原集合a一共有3个元素
 
思路: 首先我们发现原集合中0的个数是好求的,2^num[0]=cnt[0].那么怎样求剩下的元素呢?
发现如果我们一个一个从小到大求,开一个数组sum[i]表示求到当前位置之前子集和为i的子集个数.
有 num[i]*sum[0]+sum[i]=cnt[i],也就是说一共和为i的子集个数等于集合中数字i的个数乘和为0的子集个数再加上之前那些由>0&&<i的元素构成的子集中和为i的个数
然后我们每次求出一个num[i]后,用完全背包的思想将这num[i]个i一个一个的加入到集合中,用完全背包的思想更新sum数组就行了
 代码如下:
 #include <bits/stdc++.h>

 using namespace std;
typedef long long ll;
const int maxn = 5e5+;
int n,m;
ll cnt[maxn];
ll sum[maxn];
int num[maxn];
int main()
{
//freopen("de.txt","r",stdin);
int T;
scanf("%d",&T);
while (T--){
memset(sum,,sizeof sum);
memset(num,,sizeof num);
scanf("%d%d",&n,&m);
for (int i=;i<=m;++i)
scanf("%lld",&cnt[i]);
num[]=;
sum[]=cnt[];
while((<<num[])<cnt[]) num[]++;
for (int i=;i<=m;++i){
num[i]=(cnt[i]-sum[i])/sum[];//num[i]*sum[0]+sum[i]=cnt[i]
for (int j=;j<=num[i];++j){//一个一个的加入几个
for (int k=m;k>=i;--k){//完全背包思想更新sum
sum[k]+=sum[k-i];
}
}
}
vector<int> vec;
for (int i=;i<=m;++i){
for (int j=;j<num[i];++j)
vec.push_back(i);
}
for (int i=;i<vec.size();++i)
printf("%d%c",vec[i],i==(vec.size()-)?'\n':' ');
}
return ;
}
 

hdu 6092 Rikka with Subset (集合计数,01背包)的更多相关文章

  1. hdu 6092 Rikka with Subset(逆向01背包+思维)

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  2. HDU 6092`Rikka with Subset 01背包变形

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. hdu 6092 Rikka with Subset 01背包 思维

    dp[i][j]表示前i个元素,子集和为j的个数.d[i][j] = d[i][j] + d[i-1][j-k] (第i个元素的值为k).这里可以优化成一维数组 比如序列为 1 2 3,每一步的dp值 ...

  4. HDU 6092 Rikka with Subset

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. HDU 6092 Rikka with Subset(dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=6092 题意: 给出两个数组A和B,A数组一共可以有(1<<n)种不同的集合组合,B中则记录了每个数出 ...

  6. 2017 ACM暑期多校联合训练 - Team 5 1008 HDU 6092 Rikka with Subset (找规律)

    题目链接 Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, s ...

  7. hdu 6092 Rikka with Subset(多重背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6092 #include <cstdio> #include <iostream> ...

  8. HDU 6092:Rikka with Subset(dp)

    分析 很多个较小的数字可以随机组合成较大的数字,所以B数组从小到大开始遍历,除了空集,最小的那个存在的个数对应的数字必然是a数组中的数字. 每求出这一部分之后,更新后续的B序列. 分析完后,主要的难点 ...

  9. hdu 2126 Buy the souvenirs 二维01背包方案总数

    Buy the souvenirs Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. SpringBoot编程思想

    Spring Boot的特性 1).创建独立的Spring应用 2).直接嵌入Tomcat.Jetty或Undertow等Web容器(不需要部署WAR文件) 3).提供固化的starter依赖,简化构 ...

  2. Java反射学习-5 - 反射复制对象

    通过反射方式复制对象: package cn.tx.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Fi ...

  3. webbrowser控件显示word文档

    参照某网站上的步骤(http://www.kuqin.com/office/20070909/968.html)首先,在Visual Studio中创建一个C#语言的Windows应用程序,然后在左侧 ...

  4. LOJ 3124 「CTS2019 | CTSC2019」氪金手游——概率+树形DP

    题目:https://loj.ac/problem/3124 看了题解:https://www.cnblogs.com/Itst/p/10883880.html 先考虑外向树. 考虑分母是 \( \s ...

  5. SyntaxError: missing ] after element list

    在前端页面js报错,找了很久没找到原因. 后来发现是后台向前端输出json字符串,而前端接收是html格式,需要将后台json字符串改成正常字符串就可以输出,或者通过ajax的方式接收json字符串.

  6. Hive if和coalesce函数

    链接:https://blog.csdn.net/qq_26442553/article/details/79465417 if 函数举例:

  7. Maven Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

    因此:maven中,配置文件的读取应有下列红字两部分,缺一不可. 不是maven项目时,可以都不写........ <context-param> <param-name>co ...

  8. Action.c(28): Error -27796: Failed to connect to server "xxxx": [10060] Connection timed out

    Error -27796: Failed to connect to server "125.93.51.230:8080": [10061] Connection refused ...

  9. css颜色单位

    /* 用颜色的单词表示不同的颜色:red, green, blue等等 */ p { background-color: red; } /* 用rgb三元色表示,rgb => red, gree ...

  10. Vue小白篇 - Vue 的指令系统 (1) v-text、v-html

    v-text:相当于innerText v-html:相当于innerHTML <div id="box"> {{ msg }} <div v-text=&quo ...