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根据字典序算第m个子集#include <stdio.h> int main() { int n,i,j,x[21]; __int64 m,f[30]={0},t; for(i=1;i<21;i++) { f[i]=(i-1)*f[i-1]+1; //每组序列的个数(每组个数相等). } while(scanf("%d%I64d",&n,&m)!=EOF) { for(i=0;i<=n;i++) { x[i]=i; //组数 } while(n-- && m) { t=m/f[n+1]+(m%f[n+1]?1:0); //找出第m个序列位于哪一组。 printf("%d",x[t]); //是第几组则第一个数字就是几。 for(i=t;i<=n;x[i]=x[i+1],i++); { m-=(t-1)*f[n+1]+1; } putchar(m?' ':'\n'); } } return 0; }