HDU 2062 Subset sequence (xjb搞)

Subset sequence

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

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
linle   |   We have carefully selected several similar problems for you:  2065 2066 2068 2069 2064 
题解:
因为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}
可以发现,可以将这些数集分成n组,然后这些组又可以分成几个小组,然后逐个输出。
∴f(n) = n[f(n-1) + 1]。(总个数f(n))
f(1) = 1。
先求每一组的个数g(n):
不难得出:g(n) = f(n) / n。
∵f(n) = n[f(n-1) + 1]。
∴g(n) = n[f(n-1) + 1] / n = f(n-1) + 1。
∵f(n-1) = (n-1) * g(n-1)。
∴g(n) = (n-1) * g(n-1) + 1。

AC代码:
#include<bits/stdc++.h> 
using namespace std;
int main()
{
    int a;
    long long b;
    long long g[30];
    g[1]=1;
    g[2]=2;
    for(int i=3;i<22;i++)
    {
        g[i]=g[i-1]*(i-1)+1;//求出g;
    }
    
    int num[30];
    while(scanf("%d%I64d",&a,&b)!=EOF) 
    {
        memset(num,0,sizeof(num));
        for(int i=1;i<=a;i++)
            num[i]=i;    //将要输出的数字保存在数组里
        int A=a;
        while(a--)//总共有a个数
        {
            int n=b%g[a+1]? b/g[a+1]+1: b/g[a+1];  //用b取余对应的b/g,得到要输出的数在第几组
            //cout<<endl<<"n="<<n<<endl;
            if(num[n]==0)
                break;
                
          //输出对应的组代表的数字
            if(A-1==a)
            printf("%d",num[n]);       
            else
            printf(" %d",num[n]);
            for(int i=n;i<=a;i++)
                num[i]=num[i+1];            //将刚输出的数字删掉,留下剩下的数字,没用的数字都变成0了
            //b表示在剩余子集中位于第几个,减去(n-1组总子集数+1)
            b=(b-(g[a+1]*(n-1)+1));   
            //cout<<endl<<"b="<<b<<endl;
        }
        printf("\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值