HDU2062 Subset sequence

来源:互联网 发布:手机淘宝红包设置 编辑:程序博客网 时间:2024/06/06 16:58

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 12 12 22 32 43 10
 

Sample Output
111 222 12 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;}


0 0
原创粉丝点击