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. 【FPGA】 007 --Verilog中 case,casez,casex的区别

    贴一个链接:http://www.cnblogs.com/poiu-elab/archive/2012/11/02/2751323.html Verilog中  case,casez,casex的区别 ...

  2. PWN入门的入门——工具安装

    安装pwntool: 命令行运行: pip install pwntools python import pwn pwn.asm("xor eax,eax") 出现'1\xc0'  ...

  3. bugku | 你从哪里来

    题目链接 之前一直以为要用x-forwarded-for ,谁道用的是referer,Orz.在此特地记录,x-forwarded-for 和 referer的区别 X-Forwarded-For(X ...

  4. rabbitmqadmin命令行管理工具-4

    rabbitmqadmin命令行管理工具原文地址: https://www.cnblogs.com/wuzhiyuan/p/6856985.htmlhttps://www.cnblogs.com/mr ...

  5. QTP学习笔记--Excel数据源

    直接读取Excel表格的function摘自此处http://www.51testing.com/html/40/307440-827863.html  特此感谢! Excel作为QTP自动化测试的数 ...

  6. 如何在列表,字典,集合中,根据条件筛选数据 -- Python数据结构与算法相关问题与解决技巧

    实际案例: 1.过滤掉列表 [3,9,-1,10,20,-2..]的负数 2.筛出字典{'LiLei':79,'Jim':88,'Lucy':92...}中值高于90的项 3.筛出集合 {77,89, ...

  7. LeetCode 102. Binary Tree Level Order Traversal 动态演示

    按层遍历树,要用到queue class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { ...

  8. docker使用记录二:mysql安装与配置

    docker 安装mysql 和挂载 仓库位置: https://hub.docker.com/_/mysql/ 安装的同时挂载data资料卷和config 配置的资料卷刀磁盘上 docker run ...

  9. android ndk 编译 libevent

    1. 下载 libevent 2.1.8 版本 https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/ ...

  10. python 装饰器 第六步:带有收集参数的函数的装饰器

    #第六步:带有收集参数的函数的装饰器 #装饰器函数 def kuozhan(func): #内部函数(扩展之后的eat函数) def neweat(*w,**n): #以下三步就是扩展之后的功能,于是 ...