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

参考链接:很妙的递归题(“妙啊”的橘子猫微笑)

最终AC代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int maxn = 22;
LL id[maxn]={0}, f[maxn]={0};
bool flag;
void getAnser(LL n, LL m){
    if(m==0) return ; //当前元素为空集 达到临界点
    LL pos=(m-1) / (f[n-1]+1) + 1;
    //加 1 是因为下标从1开始  f[n-1]+1 表示该组的元素个数  m-1保证除后的结果在 0~n-1范围
    if(flag) printf(" ");
    else flag = true;
    printf("%lld", id[pos]);
    id[pos] = 99;
    sort(id+1, id+n+1);
    getAnser(n-1, (m-1) % (f[n-1]+1)); //(m-1) % (f[n-1]+1) 结果若为 0 则表示下一元素为空集
}
int main(){
    LL i, n, m;
    for(i=1; i<maxn; i++) f[i] = (f[i-1]+1) * i;
    while(scanf("%lld %lld", &n, &m) != EOF){
        flag = false; //记录第一个输出的数字
        for(i=1; i<=n; i++) id[i] = i;
        getAnser(n, m);
        printf("\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/heyour/p/12592448.html

时间: 2024-10-05 07:46:15

2062 Subset sequence的相关文章

HDU 2062 Subset sequence 数位dp,思路 难度:1

http://acm.hdu.edu.cn/showproblem.php?pid=2062 Subset sequence Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3569    Accepted Submission(s): 1802 Problem Description Consider the aggregate An=

HDU 2062 Subset sequence

我是把它当做一道数学题来做的. 这篇题解写的有点啰嗦,但是是我最原始的思维过程. 对于一个集合An= { 1, 2, …, n },在n比较小的情况下,在纸上按字典顺序把所有子集排列一下. 以n=3,m=10举例: 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=3的情况 容易看出前5个打头的是1,紧接着5个子集打头的是2,最后5个开头的是3. 拿前五个来说,除了第一个,后面四个不看开头的1,后面的排列形式和

hdu(2062)-Subset sequence 组合数学

题意:求集合{1,2,3...n}的第m个排列子集合.集合的大小按字典树排. 例两个元素的排列子集合按字典树排列是:{1},{1,2},{2},{2,1}: 解法:一个一个元素来确定,每次把剩余的元素按大小顺序排列在num中,然后根据排列组合原理直接计算下一个位置的元素的大小,直到排列数为0停止: 代码: /****************************************************** * author:xiefubao **********************

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 Submission(s): 1740 Problem Description Consider the aggregate An= { 1, 2, -, n }. For example, A1={1}, A3={1,2,

HDU2062 Subset sequence

Subset sequence Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3447    Accepted Submission(s): 1741 Problem Description Consider the aggregate An= { 1, 2, -, n }. For example, A1={1}, A3={1,2,

【康拓逆展开】HDU2062Subset sequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2062 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 le

hduoj 2062Subset sequence

Subset sequence Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4123    Accepted Submission(s): 2019 Problem Description Consider the aggregate An= { 1, 2, …, n }. For example, A1={1}, A3={1,2,3

Hdoj 2062

原题链接 描述 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. 输入 The inpu

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116