杭电ACM HDU 2062 Subset sequence(java)

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 1
2 3 1

import java.util.*;
public class Num2062 {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n=sc.nextInt();
            long m=sc.nextLong();
            List list=new LinkedList();
            List list1=new ArrayList();
            long num[]=new long[21];
            num[1]=1;num[2]=2;
            for(int i=3;i<n+1;i++){
                num[i]=num[i-1]*(i-1)+1;
            }
            list.add(0);
            for(int i=1;i<=n;i++){
                list.add(i);
            }
            for(int i=n;i>0;i--){
                if(m%num[i]==0){
                    list1.add(list.get((int) (m/num[i])));
                    list.remove(list.get((int) (m/num[i])));
                    m=num[i]-1;
                }
                else {
                    list1.add(list.get((int) (m/num[i])+1));
                    list.remove(list.get((int) (m/num[i])+1));
                    m=m%num[i]-1;
                    if(m==0)break;
                }
            }
            for(int i=0;i<list1.size();i++){
                if(i==0)System.out.print(list1.get(i));
                else System.out.print(" "+list1.get(i));
            }
            System.out.println();


        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值