hdu 2062 Subset sequence【有点康拓展开的意思】

简介: Subset sequence Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3441    Accepted Submis...

Subset sequence

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


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
题意:给出n和m,然后有很多个集合{1,2,,,,n}的非空子集,按照一定方式排列,例如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
然后输出第m个序列。
结题思路,仔细观察可以看出,1、2、3开头的集合个数是一样多的,因此那m/(每组个数)可以得出第一个数字,然后依次类推即可
#include<cstdio>
#include<iostream>
#define LL long long int
using 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;
}
目录
相关文章
LeetCode 60. Permutation Sequence
集合[1,2,3,...,n]总共包含n的阶乘个独特的排列。
65 0
LeetCode 60. Permutation Sequence
LeetCode 216. Combination Sum III
找出所有相加之和为 n 的 k 个数的组合。组合中只允许含有 1 - 9 的正整数,并且每种组合中不存在重复的数字。
97 0
LeetCode 216. Combination Sum III
LeetCode 39. Combination Sum
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的数字可以无限制重复被选取。
67 0
LeetCode 39. Combination Sum
uva 10706 - Number Sequence
点击打开链接uva 10706 题目意思:    有一个数组 s[1] = 1 , s[2] = 1 2 , .......s[k] = 1....k,要求给定一个n表示数组的第几位,要求这个第几位是什么数。
944 1
LeetCode 216 Combination Sum III(Backtracking)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51935033 翻译 找出所有的k个数字相加得到数字n的组合,只有1到9的数字可以被使用,并且每个组合间需要是不同的数字集。
724 0
LeetCode - 39. Combination Sum
39. Combination Sum  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个待选集合s和一个数n,让你找出集合s中相加之和为n的所有组合.
817 0
LeetCode - 40. Combination Sum II
40. Combination Sum II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个待选集合s和一个数n,选出所有相加之和为n的组合.
999 0