Rikka with Subset HDU - 6092 (DP+组合数)
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+组合数)的更多相关文章
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- 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 ...
- HDU 6092 Rikka with Subset
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 6092 Rikka with Subset(逆向01背包+思维)
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- HDU 6092`Rikka with Subset 01背包变形
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 2017杭电多校第五场Rikka with Subset
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- Rikka with Subset
Rikka with Subset Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU 5928 DP 凸包graham
给出点集,和不大于L长的绳子,问能包裹住的最多点数. 考虑每个点都作为左下角的起点跑一遍极角序求凸包,求的过程中用DP记录当前以j为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...
随机推荐
- The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server "SDSSDFCC...
The operation could not be performed because OLE DB provider "SQLNCLI11" for linked server ...
- 爬取mzi.com妹子图片网站(requests库)
看了崔大佬的文章,写了这个爬虫,学习了!原文地址 现在该网站加了反爬机制,不过在headers里加上refere参数就行了. 以下代码仅做学习记录之用: from bs4 import Beautif ...
- django加密解密api
分别给出了两个API,一个创造密码,一个验证密码正好满足需求.于是赶紧试试: 首先,引入模块: 1 >>> from django.contrib.auth.hashers impo ...
- Cs231n课堂内容记录-Lecture 4-Part1 反向传播及神经网络
反向传播 课程内容记录:https://zhuanlan.zhihu.com/p/21407711?refer=intelligentunit 雅克比矩阵(Jacobian matrix) 参见ht ...
- ASP.NET -- WebForm -- HttpResponse 类的方法和属性
ASP.NET -- WebForm -- HttpResponse 类的方法和属性 1. HttpResponse 类的方法 (1) AddCacheDependency: 将一组缓存依赖项与响应关 ...
- June 5. 2018 Week 23rd Tuesday
Learn to let go and be clear of where you really want to head for. 学会放手,同时也要弄清楚自己的真正所爱. From Kissing ...
- js中split()方法得到的数组长度
js 中split(",")方法通过 ”,“ 分割字符串, 如果字符串中没有 “,” , 返回的是字符串本身 var str = “abc”://分隔符个数为0 var newSt ...
- Teradata 批量查找PI字段
select * from dbc.indicesv where indextype in ('P','Q');
- 【SDOI2014】向量集
[SDOI2014]向量集 题目描述 我们分析一波: 假设我们询问\((A,B)\),\(x_i>x_j\)若 \[ A\cdot x_i+B\cdot y_i>A\cdot x_j+B\ ...
- Python3新特性 类型注解 以及 点点点
Python3新特性 类型注解 以及 点点点 ... Python3 的新特性 Python 是一种动态语言,变量以及函数的参数是 不区分类型 的 在 函数中使用类型注解 相当于 给 形参的 类型 设 ...