Hdu 6092 Rikka with Subset【背包Dp】

22 篇文章 1 订阅

Rikka with Subset

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


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  A1An  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  i s he got as  Bi .

Yuta shows Rikka the array  Bi  and he wants Rikka to restore  A1An .

It is too difficult for Rikka. Can you help her?  
 

Input
The first line contains a number  t(1t70) , the number of the testcases. 

For each testcase, the first line contains two numbers  n,m(1n50,1m104) .

The second line contains  m+1  numbers  B0Bm(0Bi2n) .
 

Output
For each testcase, print a single line with  n  numbers  A1An .

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

题目大意:


给你2^n子序列的和的分步情况【0,m】,Bi表示和为i的子序列的个数为Bi个。

让你找到一个原序列,以最小字典序输出。


思路:


我们贪心的去想,一开始的答案序列为空,如果B1>0,那么显然我们想要在结果序列中加一个1,直到当我们的答案序列中的Dp1==B1的时候,我们考虑找下一个位子,如果B2!=Dp2,那么显然我们希望在结果序列中加一个2,依次类推,我们特判一下0,然后加数的过程中拿一个背包去跑即可。


队长Ac代码(偷懒懒得再写一遍了):


#include <bits/stdc++.h>
typedef long long int LL;
using namespace std;


const int N = 1e6+7;
const int MOD = 1e9+7;
const int INF = 1e9+7;

inline int read(){
    int x=0,f=1;char c=getchar();
    for(;c<'0'||'9'<c;c=getchar())if(c=='-')f=-1;
    for(;'0'<=c&&c<='9';c=getchar())x=(x<<3)+(x<<1)+c-'0';
    return x*f;
}

/********************************************/

LL n,m;

LL a[333],b[111111];
LL h[111111];
LL dp[111111];

int main(){
    int _ = 1,kcase = 0;
    for(scanf("%d",&_);_--;){
        memset(dp,0,sizeof(dp));
        scanf("%lld%lld",&n,&m);
        for(int i=0;i<=m;i++) scanf("%lld",&b[i]);
        for(int i=1;i<=m;i++) b[i]/=b[0];
int l = 0;
        b[0]>>=1;
        while(b[0]){
            a[++l]=0;
            b[0]>>=1;
        }

        dp[0]=1;
        for(int i=0;i<=m&&l<n;i++){
            while(dp[i]<b[i]&&l<n){
                a[++l]=i;
                for(int j=m;j>=i;j--)
                    dp[j]+=dp[j-i];
            }
        }
        for(int i=1;i<=l;i++) printf("%lld%c",a[i],(i==l)?'\n':' ');
    }
    return 0;
}











  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值