Rikka with Subset

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

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
 
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
Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6095 6094 6093 6092 6091 
 
 
题目大意:
有一个数列 a[] ,长度(n<=50)。b[i] 表示元素和为 i 的集合个数。给你一个数列 b[] ,长度(m<=10000),让你求 a[],并按照其字典序最小输出。
题解:
网上有说是01背包,然而我也不知道自己写了点什么,大概是01背包吧
正常的背包:告诉你这个重量的物品个数,让你装包
逆向背包:这题告诉你,得到的这个重量有多少种,求各个重量的物品个数
 
官方题解:

1008 Rikka with Subset

签到题,大致的思想就是反过来的背包。

如果 B​i​​ 是 B 数组中除了 B​0​​ 以外第一个值不为 0 的位置,那么显然 i 就是 A 中的最小数。

现在需要求出删掉 i 后的 B 数组,过程大概是反向的背包,即从小到大让 Bj-=B​(j−i)​​。

时间复杂度 O(nm)。

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<vector>
using namespace std;
int t,k;
int n,m;
int bb[],b[],a[];
int main()
{
scanf("%d",&t); while(t--)
{
scanf("%d%d",&n,&m);
memset(a,,sizeof(a));//a[i]表示第i个数字的个数
memset(bb,,sizeof(bb));//bb[k]表示当1~k-1中数字个数确定后,凑到和为k的种数,不够就表示,需要单独k这个数字来凑 for(int i=;i<=m;i++)
scanf("%d",&b[i]); k=; bb[]=;
while(k<=m)
{
a[k]=b[k]-bb[k]; //这就是因为1~k-1个数确定后,能凑到和为k的种数,不够的说明a序列中有b[k]-bb[k]个数的k for(int j=;j<=a[k];j++)
{
for(int i=m;i>=k;i--) //反着来,避免已经加到结果里的数字再加一遍,这里有01背包的感觉
bb[i]+=bb[i-k];
}
k++;
} int tot=; //输出
for(int i=;i<=m;i++)
for(int j=;j<=a[i];j++)
{
if (tot++) printf(" ");
printf("%d",i);
}
printf("\n");
}
return ;
}

hdu 6092 Rikka with Subset(逆向01背包+思维)的更多相关文章

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

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

  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 (集合计数,01背包)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  6. HDU 6092 Rikka with Subset(dp)

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

  7. 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 ...

  8. HDU 6092:Rikka with Subset(dp)

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

  9. HDU 3639 Bone Collector II(01背包第K优解)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. Winter-2-STL-B Brackets 解题报告及测试数据

    Time Limit:2000MS     Memory Limit:65536KB Description Given a string consisting of brackets of two ...

  2. CSS清除浮动大全的8种方法

    清除浮动是每一个 web前台设计师必须掌握的机能.css清除浮动大全,共8种方法. 浮动会使当前标签产生向上浮的效果,同时会影响到前后标签.父级标签的位置及 width height 属性.而且同样的 ...

  3. node异步流程控制async

    1.串行无关联:async.series(tasks,callback); 多个函数依次执行,之间没有数据交换,其中一个函数出错,后续函数不再执行 async.series({ one: functi ...

  4. 337APuzzles

    dangerous /*大水题目.不解释 给你m个数,从中选出n个,保证最大值和最小值的差值最小, 做法:从小到大排序,然后暴力枚举每个长度是n的序列*/ #include<stdio.h> ...

  5. 【Head First Servlets and JSP】笔记 28: 过滤器与包装器

    1.过滤器的执行顺序: <url-pattern> 为第一梯队, <servlet-name> 为第二梯队,梯队内的执行顺序和 DD 里的声明顺序相同. When the co ...

  6. ElasticSearch(三) ElasticSearch中文分词插件IK的安装

    正因为Elasticsearch 内置的分词器对中文不友好,会把中文分成单个字来进行全文检索,所以我们需要借助中文分词插件来解决这个问题. 一.安装maven管理工具 Elasticsearch 要使 ...

  7. lamp编译详解

    首先确认系统环境:centos6.4 min版本 1.安装需要的开发环境 yum groupinstall "Development Tools" "Server Pla ...

  8. 超详细 Spring @RequestMapping 注解使用技巧 (转)

    @RequestMapping 是 Spring Web 应用程序中最常被用到的注解之一.这个注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上. 在这篇文章中,你将会看到 @R ...

  9. 回到HTML〇

    HTML(HyperText Markup Language),用来向浏览器标示文档的所有“内容”与“结构”. 抱着温故而知新的态度,在这里通过“回到HTML”系列文章,重新梳理一下HTML的相关知识 ...

  10. 关于office word 应用程序下载配置

    Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} fai ...