Subset sequence
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2788 Accepted Submission(s): 1461
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=3时,有
{1}
{1, 2}
{1, 2, 3}
{1, 3}
{1, 3, 2}
{2}
{2, 1}
{2, 1, 3}
{2, 3}
{2, 3, 1}
{3}
{3, 1}
{3, 1, 2}
{3, 2}
{3, 2, 1}
第10组在,第二组里所以输出第一个为2,然后删除2,剩下1,3;所以分成2组,第2组为3所以输出3,删掉3还剩1然后输出1;
代码如下:
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int i,n,t;
__int64 m,c[21]={0};
int s[21];
for(i=1 ;i<=21; i++)
{
c[i]=c[i-1]*(i-1)+1;//可分为c[n]组数据
}
while(scanf("%d%I64d",&n,&m)!=EOF)
{
for(i=1;i<=21;i++)
s[i]=i;
while(n>0&&m>0)
{
t=m/c[n]+(m%c[n]>0?1:0);
if(t>0)
{
cout<<s[t];
for(i=t;i<=n;i++)
s[i]=s[i+1];
m-=((t-1)*c[n]+1);
if(m==0)cout<<endl;
else cout<<' ';
}
n--;
}
}
return 0;
}