hdu 2062 Subset sequence

Description

Consider the aggregate An= { 1, 2, …, n }. For example, A1={1}, A3={1,2,3}. A subset sequence is defined as a array of a non-empty subset. Sort all the subset sequece of An in lexicography order. Your task is to find the m-th one.

Input

The input contains several test cases. Each test case consists of two numbers n and m ( 0< n<= 20, 0< m<= the total number of the subset sequence of An ).

Output

For each test case, you should output the m-th subset sequence of An in one line.

Sample Input

1 1
2 1
2 2
2 3
2 4
3 10

Sample Output

1
1
1 2
2
2 1
2 3 1

分析:一开始我是画横向多叉树来分析的,发现是一个树枝递减的多叉树,编码后计算出上下结点间的数字差,发现符合f(n)=(n-1)*(f(n-1)+1),之后想按照将m%f(n)来确定每一个结点的数字,然而计算时没有考虑亮点:
1.每次算出该结点对应的数字后,在计算下一结点时,需取完余数后再减去1,这是因为上一个已算出的结点也算入余数中,计算后需减去;
2.在余数为0的时候,没有考虑到这时这个这个终点应该上面的一棵树上,而不是m/f(n)得出的那棵树
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
long long sub[25] = {0}, str[25];
int main()
{
	int i, j, n;
	long long t, m;
	for (i = 1; i <= 20; i++)
		sub[i] = sub[i - 1] * (i-1) + 1;
	while (scanf("%d%lld", &n, &m) != EOF)
	{
		for (i = 0; i <= 20; i++)
			str[i] = i;
		while (n > 0 && m > 0)
		{
			t = m / sub[n]+(m%sub[n]>0?1:0);
			if (t > 0)
			{
				printf("%d", str[t]);
				for (i = t; i < n; i++)
					str[i] = str[i + 1];
				m -= (sub[n] * (t - 1) + 1);
				printf(m > 0 ? " ":"\n");
			}
			n--;
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值