1. 程式人生 > >*寒假水58——Subset sequence

*寒假水58——Subset sequence

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. 

InputThe 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 ).OutputFor 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
#include<cstdio>  
#include<cstring>  
#include<cmath>  
long long sub[25] = {0}, str[25];  
int main()  
{  
    int i, j, n;  
    long long t, m;  
    for (i = 1; i <= 20; i++)  
        sub[i] = sub[i - 1] * (i-1) + 1;  
    while (scanf("%d%lld", &n, &m) != EOF)  
    {  
        for (i = 0; i <= 20; i++)  
            str[i] = i;  
        while (n > 0 && m > 0)  
        {  
            t = m / sub[n]+(m%sub[n]>0?1:0);  
            if (t > 0)  
            {  
                printf("%d", str[t]);  
                for (i = t; i < n; i++)  
                    str[i] = str[i + 1];  
                m -= (sub[n] * (t - 1) + 1);  
                printf(m > 0 ? " ":"\n");  
            }  
            n--;  
        }  
    }  
    return 0;  
}  

題解:感覺自己的跟別人的一模一樣,怎麼就人家的能過我不能。。。。。。哼!