Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2062
Problem Descriptionconsider 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 a in lexicography order. Your task is to find the m-th one.
Inputthe input contains several test cases. Each test case consists of a numbers n and m (0< n<=, 0< m<= The total number of the subset sequence of AN).
Outputfor Each test case, you should output the m-th subset sequence of a in one line.
Sample Input
1 12 12 22 32 43 10
Sample Output
111 222 12) 3 1
We can use vector to realize the inverse expansion of Kang extension;
Code:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include < Vector> #define LL Long longusing namespace Std;int ans[30];int len=0; LL fac[23];/* The inverse expansion has acvoid Cantor (int n,ll m) {int n=n; int i,j,vis[23]={0}; while (n--&&m) {LL t=m/fac[n+1]; if (m%fac[n+1]) t+=1; LL k=t; for (i=1;i<=n;i++) {if (!vis[i]) {k--; if (k==0) break; }} vis[i]=1; Ans[len++]=i; m-= ((t-1) *fac[n+1]+1); }}*///Cantor (int n,ll m) {vector<int>q;) with vector implementation for (int i=1;i<=20;i++) {q.push_back (i); } while (n--&&m) {LL t=m/fac[n+1]; if (m%fac[n+1]) t+=1; ANS[LEN++]=Q[T-1]; Q.erase (Q.begin () +t-1); m-= ((t-1) *fac[n+1]+1); }}void f () {fac[0]=0; Fac[1]=1; Note the data type; for (int i=2;i<=21;i++) {fac[i]=fac[i-1]* (i-1) +1; }}int Main () {f (); LL N,m; Freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); Cin.sync_with_stdio (FALSE); while (cin>>n>>m) {len=0; Cantor (N,M); for (int i=0;i<len;i++) {if (i<len-1) cout<<ans[i]<< "; else cout<<ans[i]<<endl; }} return 0;}
"Comtop Inverse expansion" hdu2062subset sequence