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): 1536 Accepted Submission(s): 776

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
2017 Multi-University Training Contest - Team 5

题意:
就是给你a数组的大小n和b数组的大小m,a数组是原始的数组
又给你b数组中的数,b数组记录着a数组的各个集合的和
b[i]代表a数组中和为i的集合的个数是b[i];

思路:
///大致的思想就是反过来的背包。
如果 B_i是B数组中除了B_0以外第一个值不为0的位置,那么显然i就是A中的最小数。
现在需要求出删掉i后的B数组,过程大概是反向的背包,即从小到大让 B[j]-=B[j-i];
时间复杂度 O(nm)
按照题解来说,那么解题思路就是从b数组下手从小到大找到第一个不为零的数,就是a数组中的数之一,
但是b数组中的其他数中可能包含这个数,我们把这个数对其余数造成的影响排除,即如果包含这个数就减去
再依此类推找a数组中的其余数,找到n-1个数的时候就结束了.
那么如何在找到一个数的时候,消除这个数对整个b数组的影响呢。这就用到了背包,例如就像我们找到了a数组中的一个数i,
那么对于b数组中一个为j的数:子集和为j的个数=子集和为j(包含i)的个数+子集和为j(不包含i)的个数
子集和为j的个数就是b[j],子集和为j(包含i)的个数是b[j-i],因为他们是由(j-i)+i得到,个数自然就为b[j-i]
那么不难推出:b[j]=b[j]-b[j-i];
再推荐一篇讲得比较细的博客:戳一下
代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        int a[55],b[10005];
        scanf("%d %d",&n,&m);
        for(int i=0;i<=m;i++)
            scanf("%d",&b[i]);
        int k=0;
        for(int i=1;i<=m;)
        {
            if(k>=n)
                break;
            if(b[i]!=0)
            {
                a[k++]=i;
                for(int j=i;j<=m;j++)
                    b[j]=b[j]-b[j-i];
            }
            else
                i++;
        }
        for(int i=0;i<n-1;i++)
            printf("%d ",a[i]);
        printf("%d\n",a[n-1]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值