hdu 2062 Subset sequence

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
根据字典序算第m个子集
#include <stdio.h>
int main()
{
    int n,i,j,x[21];
    __int64 m,f[30]={0},t;
    for(i=1;i<21;i++) 
    {
        f[i]=(i-1)*f[i-1]+1;  //每组序列的个数(每组个数相等).
    }
    while(scanf("%d%I64d",&n,&m)!=EOF)
    {
        for(i=0;i<=n;i++) 
        {
            x[i]=i;    //组数
        }
        while(n-- && m)
        {
            t=m/f[n+1]+(m%f[n+1]?1:0);  //找出第m个序列位于哪一组。
            printf("%d",x[t]);         //是第几组则第一个数字就是几。
            for(i=t;i<=n;x[i]=x[i+1],i++);    
            {
                m-=(t-1)*f[n+1]+1;
            }
            putchar(m?' ':'\n');
        }                                     
    }
    return 0;
}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值