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 nn positive A1−AnA1−An and their sum is mm. Then for each subset SS of AA, Yuta calculates the sum of SS.

Now, Yuta has got 2n2n numbers between [0,m][0,m]. For each i∈[0,m]i∈[0,m], he counts the number of iis he got as BiBi.

Yuta shows Rikka the array BiBi and he wants Rikka to restore A1−AnA1−An.

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

InputThe first line contains a number t(1≤t≤70)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)n,m(1≤n≤50,1≤m≤104).

The second line contains m+1m+1 numbers B0−Bm(0≤Bi≤2n)B0−Bm(0≤Bi≤2n).OutputFor each testcase, print a single line with nn numbers A1−AnA1−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]$

题意:一个含有n个数的数组,他们的sum和是m,并且这n个数的所有子集的sum和的个数用一个数组b来表示。
其中b[i] 表示 子集中sum和为i的有b[i]个。现在给你N,M,b数组,让你推出数组a,并输出字典序最小的那一个。 思路:可以知道满足条件的只有一个数集set,所以只需要排序输出就是字典序最小的那个了。
那么如何求数组a呢?,首先我们应该知道,如果有n个0,会产生2^n个sum和为0的集合。
那么数组a中0的数量直接就是log2(b[0])了。
而sum和为1的只需要用所以的sum和为0的集合加上一个1即可,
所以num[1] = b[1]/b[0];
然后定义数组dp[i],表示不用数字i,仅用小于i中的数凑出来sum和为i的集合数量。
那么(b[i]-dp[i])/b[0] 就是Num[i]
求dp[i]的过程中用到dp的思想,细节见代码。
    if (dp[j] == ) continue;
if (num[i] == ) break;

这步的代码可以节省时间复杂度。

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = ; while (b) {if (b % )ans = ans * a % MOD; a = a * a % MOD; b /= ;} return ans;}
inline void getInt(int* p);
const int maxn = ;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int t;
int n, m;
ll b[maxn];
int dp[maxn];
int num[maxn];
int c(int n, int m)
{
int sum = ;
for (int i = n - m + ; i <= n; i++) sum *= i;
for (int i = ; i <= m; i++) sum /= i;
return sum;
}
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
// gbtb;
// cin >> t;
scanf("%d", &t);
while (t--)
{
MS0(num);
MS0(dp);
scanf("%d%d", &n, &m);
repd(i, , m)
{
scanf("%lld", &b[i]);
// cin >> b[i];
}
num[] = log2(b[]);
num[] = b[] / b[];
dp[] = b[];
repd(i, , m)
{
for (int j = m; j >= ; j--)
{
if (dp[j] == ) continue;
if (num[i] == ) break;
for (int k = ; k <= num[i]; k++)
{
if (j + k*i <= m)
{
dp[j + k*i] += dp[j] * c(num[i], k);
}
}
}
if (i + <= m)
{
num[i+]=(b[i+]-dp[i+])/b[];
}
}
bool flag = ;
for (int i = ; i <= m; i++)
{
for (int j = ; j <= num[i]; j++)
{
if (!flag) printf("%d", i), flag = ;
else printf(" %d", i);
}
}
puts("");
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
												

Rikka with Subset HDU - 6092 (DP+组合数)的更多相关文章

  1. hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)

    DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...

  2. 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 ...

  3. HDU 6092 Rikka with Subset

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  4. hdu 6092 Rikka with Subset(逆向01背包+思维)

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. HDU 6092`Rikka with Subset 01背包变形

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. 2017杭电多校第五场Rikka with Subset

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  7. Rikka with Subset

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  8. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. HDU 5928 DP 凸包graham

    给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...

随机推荐

  1. python连接sqlserver数据库

    1.准备工作 python3.6连接sqlserver数据库需要引入pymssql模块 pymssql官方:https://pypi.org/project/pymssql/ 没有安装的话需要: pi ...

  2. c/c++ 智能指针 unique_ptr 使用

    智能指针 unique_ptr 使用 和shared_ptr不同,可以有多个shared_ptr指向同一个内存,只能有1个unique_ptr指向某个内存.因此unique_ptr不支持普通的拷贝和赋 ...

  3. mybatis根据数据库表结构自动生成实体类,dao,mapper

    首先, pom需要引入 <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifac ...

  4. 【字符串】ZSC-勤奋的计算机系学生

    Description 计算机系的同学从大一就开始学习程序设计语言了.初学者总是容易写出括号不匹配的程序.至今你仍然清楚地记得,那天上机的时候你的程序编译出错,虽然你使尽了吃奶的力气也没有把错误逮着. ...

  5. python list和tuple

    list列表简介:列表是python的基础数据类型之⼀ ,其他编程语⾔也有类似的数据类型. 比如JS中的数组, java中的数组等等. 它是以[ ]括起来, 每个元素⽤' , '隔开⽽且可以存放各种数 ...

  6. https 建立连接过程

    http://blog.csdn.net/wangjun5159/article/details/51510594 思考问题的顺序 学技术时,总是会问什么?这里也不例外,https为什么会存在,它有什 ...

  7. margin-left:-20px

    对于负的margin值,设置其作用时要和position:absolute;一起使用

  8. Spring 的application.properties项目配置与注解

    一.项目结构介绍 如上图所示,Spring Boot的基础结构共三个文件: src/main/java  程序开发以及主程序入口 src/main/resources 配置文件 src/test/ja ...

  9. PostgreSQL 数据库NULL值的默认排序行为与查询、索引定义规范 - nulls first\last, asc\desc

    背景 在数据库中NULL值是指UNKNOWN的值,不存储任何值,在排序时,它排在有值的行前面还是后面通过语法来指定. 例如 -- 表示null排在有值行的前面 select * from tbl or ...

  10. localhost和127.0.0.1及ip区别

    1.127.0.0.1是回送地址,指本地机,一般用来测试使用.回送地址是本机回送地址(Loopback Address),即主机IP堆栈内部的IP地址,主要用于网络软件测试以及本地机进程间通信,无论什 ...