B

来源:互联网 发布:耐驰热分析软件下载 编辑:程序博客网 时间:2024/05/02 01:27
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 12 12 22 32 43 10
Sample Output
111 222 12 3 1



#include<stdio.h>int flag;long long x[22], a[22];void f(long long n, long long m);int main(){    long long n, m, i;    for(i=1;i<21;i++)        x[i]=i*x[i-1]+i;    while(scanf("%lld%lld", &n, &m)!=EOF)    {        flag=0;        for(i=1;i<=n;i++)            a[i]=i;        f(n, m);        printf("\n");    }    return 0;}void f(long long n, long long m){    int i;    long long k;    if(m==0)        return;    k=(m-1)/(x[n-1]+1)+1;    if(flag==0)    {        printf("%lld", a[k]);        flag=1;    }    else        printf(" %lld", a[k]);    for(i=k;i<21;i++)        a[i]=a[i+1];    f(n-1, (m-1)%(x[n-1]+1));}

原创粉丝点击