Hdu 2062 Subset sequence

来源:互联网 发布:2015中国国际储备数据 编辑:程序博客网 时间:2024/05/22 14:29

Subset sequence

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
这道题就是问你用n个数组成的子集中按字典序排序第m个是多少,这感觉是一道递归题,不断求子集的子集的问题…然而我看了好多网上的答案都不明所以,大概懂了点写了个代码,还得再看看…

#include<bits/stdc++.h>using namespace std;using LL =int64_t;int main(){    ios::sync_with_stdio(0);    cin.tie(0);    LL num[25]={0,1},m;    vector<int>cnt;    for(int i=2;i<=20;i++) num[i]=(num[i-1]+1)*i;    int n;    while(cin>>n>>m) {        cnt.clear();        for(int i=1;i<=20;i++) cnt.push_back(i);        while(n--){            int k=(m-1)/(num[n]+1);            cout<<cnt[k];            cnt.erase(cnt.begin()+k);            m=(m-1)%(num[n]+1);            if(m==0) break;            else cout<<" ";        }        cout<<endl;    }    return 0;}
原创粉丝点击