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为当前末端为结束的的最小长度,其中一维作为背包的是凸包内侧点的数量.也 ...
随机推荐
- SQL Server 缓存清除与内存释放
Sql Server系统内存管理在没有配置内存最大值,很多时候我们会发现运行SqlServer的系统内存往往居高不下.这是由于他对于内存使用的策略是有多少闲置的内存就占用多少,直到内存使用虑达到系统峰 ...
- 在Unity中对Lua进行调试
前言 接我之前的文章,讲到使用IntelliJ IDEA(做为Lua的编辑器)+EmmlyLua(插件),当然EmmlyLua也提供调试功能的. Lua代码提示和方法跳转 在Lua中提示UnityEn ...
- C#批量向数据库插入数据
程序中,批量插入数据有两种思路. 1.用for循环,一条一条的插入,经实测,这种方式太慢了(插入一万条数据至少都需要6-7秒),因为每次插入都要打开数据库连接,执行sql,关闭连接,显然这种方式不可行 ...
- 数位dp D - Count The Bits
题目:D - Count The Bits 博客 #include <cstdio> #include <cstring> #include <cstdlib> # ...
- 修改CentOS默认yum源为国内yum镜像源
CentOS默认的yum源不是国内的yum源,在通过yum安装一些软件的时候,会出现这样那样的错误,以及在下载安装的速度上也是非常慢的. 所以这个时候就需要将yum源替换成国内的yum源,国内主要开源 ...
- UVA11846-Finding Seats Again(DFS)
Problem UVA11846-Finding Seats Again Accept: 69 Submit: 433Time Limit: 10000 mSec Problem Descrip ...
- 深度学习之Attention Model(注意力模型)
1.Attention Model 概述 深度学习里的Attention model其实模拟的是人脑的注意力模型,举个例子来说,当我们观赏一幅画时,虽然我们可以看到整幅画的全貌,但是在我们深入仔细地观 ...
- 转://如何增加linux根目录的磁盘空间(基于LVM)?
问题引出: 在测试过程中替换so文件,报磁盘空间不足的错误. ▲问题分析: 由于当时系统部署架构的考虑,把软件和数据库部署在了同一台机器上,并且给了30G的磁盘空间.系统上占用磁盘空间的有2部分,一是 ...
- PHP与Nginx之间的运行机制以及原理
一.普及Nginx与Php-fpm相关知识点 Nginx是什么 Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服 ...
- Linux:Day9(下) 程序包管理
API:Application Programming Interface POSIX:Portable OS 程序源代码 --> 预处理 --> 编译 --> 汇编 --> ...