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 }

 

转载于:https://www.cnblogs.com/Deribs4/p/4795835.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值