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. RPC细节

    服务化有什么好处? 服务化的一个好处就是,不限定服务的提供方使用什么技术选型,能够实现大公司跨团队的技术解耦,如下图所示: 服务A:欧洲团队维护,技术背景是Java 服务B:美洲团队维护,用C++实现 ...

  2. doc命令下查看java安装路径

    在doc窗口下使用命令:set  java_home 即可查看.

  3. 判断变量是否为 NaN

    首先要明确 NaN 的一个特性, NaN不能用相等操作符(== 和 ===) 来判断, NaN === NaN 会返回 false: 下面是测试代码: console.log(isNaN('1')) ...

  4. 负载均衡技术在CDN中发挥着重要作用

    转载地址:http://www.qicaispace.com/gonggao/server/page01/info07.asp CDN是一个经策略性部署的整体系统,能够帮助用户解决分布式存储.负载均衡 ...

  5. 【前端】Vue.js实现简单价格计算器

    价格计算器 实现效果: 实现代码及注释: <!DOCTYPE html> <html> <head> <title>价格计算器</title> ...

  6. LeetCode——Unique Paths

    Question A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram be ...

  7. MATLAB 图形图像处理

    theme: MATLAB author: pprp date: 2018/2/2 --- MATLAB 图形图像处理 二维绘图命令 plot 线性空间 plot(t,[x1,x2,x3]) : 在同 ...

  8. Python基础笔记系列十三:socket网络编程

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!!使用python编写一个简易的服务端程序和客户端程序,启动服务端和客户端(监 ...

  9. codeforces27D Ring Road 2

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  10. Location对象常用知识

    产品终于上线,后期主要是优化了.在开发过程中用到了很多location对象的知识,趁现在有时间先整理一下. Location 对象存储在 Window 对象的 Location 属性中,可通过wind ...