HDU2062 递推

来源:互联网 发布:淘宝转化率6是什么意思 编辑:程序博客网 时间:2024/05/29 09:26

Subset sequence

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4715    Accepted Submission(s): 2280


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 12 12 22 32 43 10
 

Sample Output
111 222 12 3 1


其实这题挺巧妙的,要找规律.

n=1

{      1


n=2

{      1

       1 2

       2

       2 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

可以发现一个规律就是上一层的全集成了下一层每一个元素后面跟着的子集,于是有了

F(n)=F(n-1)*(n-1)+1;

然后可以模运算来找出子集的第一个字符,然后在字符集合中去掉这个数,再循环上一层,一直找到第一层,然后整个结构就出来了。

#include<cstdio>#include<iostream>#define LL long long intusing namespace std;int main(){    int n,a[21],i;    LL m,t;    LL p[21]={0,1};    for(i=2;i<=20;i++){        p[i]=p[i-1]*(i-1)+1;//0、1、2、5、16    }    while(cin>>n>>m){        for(i=0;i<=20;i++)            a[i]=i;        while(n--&&m){            t=m/p[n+1]+((m%p[n+1])?1:0);            cout<<a[t];//当前的首数字            for(i=t;i<=n;i++)                a[i]=a[i+1];//回到n-1个数字,更新输出数组            m-=((t-1)*p[i]+1);//去掉前面的小于t开头的组合,且将去掉一个仅有t的集合            printf(m==0?"\n":" ");        }    }    return 0;}



0 0
原创粉丝点击