hduoj 2062Subset sequence

Subset sequence

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


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
 

 

Author
LL
 

 

Source
 

 

Recommend
linle   |   We have carefully selected several similar problems for you:  2059 2065 2056 2058 2061 

 

 

学习网址:http://blog.csdn.net/lianqi15571/article/details/8877014

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<stack>
 5 #include<set>
 6 #include<map>
 7 #include<queue>
 8 #include<algorithm>
 9 using namespace std;
10 long long c[21]={0};//c[n]表示长度为n,第一位固定,剩下数字排列形成数列的个数
11 //易知c[n]=(n-1)*c[n-1]+1
12 //含义:比如第一位是A1  剩下A2A3。。An 共n-1个数可以固定在第二位,再加上一个空集
13 bool s[22];
14 int main(){
15     //freopen("D:\\INPUT.txt","r",stdin);
16     int n,i,j;
17     long long m;
18     for(i=1;i<21;i++){
19         c[i]=(i-1)*c[i-1]+1;
20         //cout<<c[i]<<endl;
21     }
22     long long t,count;
23     while(scanf("%d %lld",&n,&m)!=EOF){
24         memset(s,false,sizeof(s));
25         j=n;
26         queue<int> q;
27         while(m){
28           count=0;
29           t=m/c[j]-(m%c[j]==0?1:0);
30           m-=t*c[j];
31           m--;//去掉一个空集!!
32           j--;
33           for(i=1;i<=n;i++){
34             if(!s[i]){
35                 count++;
36                 if(count==t+1){
37                     break;
38                 }
39             }
40           }
41           s[i]=true;
42           q.push(i);
43         }
44         printf("%d",q.front());
45         q.pop();
46         while(!q.empty()){
47             printf(" %d",q.front());
48             q.pop();
49         }
50         printf("\n");
51     }
52     return 0;
53 }

 

posted @ 2015-09-09 20:20  Deribs4  阅读(187)  评论(0编辑  收藏  举报