Subset sequence
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3005 Accepted Submission(s): 1563
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
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int n,s[21];
__int64 m;
__int64 g[21];
g[1]=1;
for(int i=2;i<=21;i++)
g[i]=(i-1)*g[i-1]+1;
while(scanf("%d%I64d",&n,&m)!=EOF)
{
int t;
for(int i=0;i<=21;i++)
s[i]=i;
while(n)
{
t=m/g[n]+(m%g[n]>0?1:0);
if(t>0)
{
printf("%d",s[t]);
for(int i=t;i<=n;i++)
s[i]=s[i+1];
m-=((t-1)*g[n]+1);
printf(m==0?"\n":" ");
}
n--;
}
}
return 0;
}

本文介绍了一种算法,用于解决给定整数集合An的所有非空子集序列按字典序排序后找到第m个的问题。输入包含多个测试用例,每个用例包括两个整数n和m。
656

被折叠的 条评论
为什么被折叠?



