Rikka with SubsetTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 139 Accepted Submission(s): 49
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 i s 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
Sample Output
|
AC代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 11000
int main()
{
int T;
int n,m;
int B[MAX],dp[MAX],num[MAX],ans[MAX];//dp[i]表示:加和为i的子集个数;数组存的是用比i小的数能组成i的情况
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(int i=0;i<=m;i++)
{
scanf("%d",&B[i]);
}
memset(ans,0,sizeof(ans));
memset(num,0,sizeof(num));
memset(dp,0,sizeof(dp));
dp[0]=1;//空集的情况
int cnt=0;
for(int i=1;i<=m;i++)
{
num[i]=B[i]-dp[i];//A序列中值为i的个数
if(cnt==n) break;
for(int j=1;j<=num[i];j++)
{
ans[cnt++]=i; //对A序列赋值
for(int k=m;k>=i;k--)//处理成01背包
dp[k]+=dp[k-i];//和为k的A子集个数相加去更新B序列
}
}
for(int i=0;i<n;i++)
printf("%d%c",ans[i],i==n-1?'\n':' ');
}
return 0;
}
Rikka with SubsetTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 139 Accepted Submission(s): 49
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 i s 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
Sample Output
|