HDU 6092 17多校5 Rikka with Subset(dp+思维)

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]$

启发博客:http://www.cnblogs.com/stepping/p/7324970.html

官方题解:

1008 Rikka with Subset

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

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

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

时间复杂度 O(nm)。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<queue>
 6 #include<cmath>
 7 #include<string>
 8 #include<map>
 9 #include<vector>
10 using namespace std;
11
12 int b[10005];//最终结果b
13 int bb[10005];//目前得出的b
14 int a[10005];
15
16 int main()
17 {
18     int T,n,m,ans;
19     scanf("%d",&T);
20     while(T--)
21     {
22         scanf("%d%d",&n,&m);
23         for(int i=0;i<=m;i++)
24             scanf("%d",&b[i]);
25         memset(a,0,sizeof(a));
26         memset(bb,0,sizeof(bb));
27         bb[0]=1;
28         for(int i=1;i<=m;i++)
29         {
30             a[i]=b[i]-bb[i];
31             for(int j=1;j<=a[i];j++)//对bb进行更新
32             {
33                 for(int k=m;k>=i;k--)//反着来避免已经加到结果里的数字再加一遍
34                     bb[k]+=bb[k-i];
35             }
36         }
37         int flag=0;
38         for(int i=1;i<=m;i++)
39             for(int j=1;j<=a[i];j++)
40             {
41                 if(flag++) printf(" ");
42                 printf("%d",i);
43             }
44         printf("\n");
45     }
46     return 0;
47 }
时间: 2024-10-09 14:55:11

HDU 6092 17多校5 Rikka with Subset(dp+思维)的相关文章

HDU 6090 17多校5 Rikka with Graph(思维简单题)

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: For an undirected graph G with n nodes and m edges, we can define the distance between 

HDU 6124 17多校7 Euler theorem(简单思维题)

Problem Description HazelFan is given two positive integers a,b, and he wants to calculate amodb. But now he forgets the value of b and only remember the value of a, please tell him the number of different possible results. Input The first line conta

HDU 6059 17多校3 Kanade&#39;s trio(字典树)

Problem Description Give you an array A[1..n],you need to calculate how many tuples (i,j,k) satisfy that (i<j<k) and ((A[i] xor A[j])<(A[j] xor A[k])) There are T test cases. 1≤T≤20 1≤∑n≤5∗105 0≤A[i]<230 Input There is only one integer T on fi

HDU 6045 17多校2 Is Derek lying?

题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 687    Accepted Submission(s): 389 Problem Description Derek and Alfia are good friends.Derek 

HDU 6077 17多校4 Time To Get Up 水题

Problem Description Little Q's clock is alarming! It's time to get up now! However, after reading the time on the clock, Little Q lies down and starts sleeping again. Well, he has 5 alarms, and it's just the first one, he can continue sleeping for a

HDU 6060 17多校3 RXD and dividing(树+dfs)

Problem Description RXD has a tree T, with the size of n. Each edge has a cost.Define f(S) as the the cost of the minimal Steiner Tree of the set S on tree T. he wants to divide 2,3,4,5,6,…n into k parts S1,S2,S3,…Sk,where ?Si={2,3,…,n} and for all d

HDU 6055 17多校 Regular polygon(计算几何)

Problem Description On a two-dimensional plane, give you n integer points. Your task is to figure out how many different regular polygon these points can make. Input The input file consists of several test cases. Each case the first line is a numbers

HDU 6047 17多校 Maximum Sequence(优先队列)

Problem Description Steph is extremely obsessed with "sequence problems" that are usually seen on magazines: Given the sequence 11, 23, 30, 35, what is the next number? Steph always finds them too easy for such a genius like himself until one da

HDU 6050 17多校2 Funny Function(数学+乘法逆元)

Problem Description Function Fx,ysatisfies:For given integers N and M,calculate Fm,1 modulo 1e9+7. Input There is one integer T in the first line.The next T lines,each line includes two integers N and M .1<=T<=10000,1<=N,M<2^63. Output For eac