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
题解:
http://blog.csdn.net/lianqi15571/article/details/8877014
看他的递推关系。
vector的练习。erase,clear,不能memset,如何加元素。
cout << '\b' << '\n' ; 是过不了编译的。
下面是代码:
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int main()
{
long long m,n;
long long f[25];
memset(f,0,sizeof(f));
f[1] = 1;
for(int i=2;i<25;i++){
f[i] = i*(f[i-1]+1);
}
vector<int> coor;
while(cin >> n >> m){
for(int i=0;i<n;i++)
coor.push_back(i+1);
int cnt = n;
while(m!=0){
long long temp = f[cnt]/cnt;
long long pos = (m-1)/temp;
cout << coor[pos] ;
coor.erase(coor.begin()+pos);
cnt--;
m = m - pos*temp -1; //减一要注意。
if(m==0)
cout << '\n';
else cout << ' ';
}
coor.clear();
}
}