码迷,mamicode.com
首页 > 其他好文 > 详细

HDU2062 Subset sequence

时间:2015-01-19 11:04:14      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:hdu2062

Subset sequence

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3447    Accepted Submission(s): 1741


Problem 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

#include <stdio.h>
#include <string.h>
#include <math.h>

__int64 an[22] = {0, 1}, m;
int box[22], vis[22];

int find_m_min(int n, int m) {
	int i, k = 0;
	for (i = 1; i <= n; ++i)
		if (!vis[i] && ++k == m) {
			break;
		} 
	vis[i] = 1; return i;
}

int main() {
	freopen("stdin.txt", "r", stdin);
	int i, n, id, j, nn, tmp;
	for (i = 2; i <= 20; ++i)
		an[i] = i * (an[i-1] + 1);
	while (scanf("%d%I64d", &n, &m) == 2) {
		memset(vis, 0, sizeof(vis));
		nn = n;
		while (n-- && m) {
			j = m / (an[n] + 1) + (m % (an[n] + 1) ? 1 : 0);
			printf("%d", find_m_min(nn, j));
			m -= (j - 1) * (an[n] + 1) + 1;
			printf("%c", m ? ' ' : '\n');
		}
	}
	return 0;
}


HDU2062 Subset sequence

标签:hdu2062

原文地址:http://blog.csdn.net/chang_mu/article/details/42869267

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!