2062 Subset sequence

Posted heyour

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("
");
    }
    return 0;
}

 

以上是关于2062 Subset sequence的主要内容,如果未能解决你的问题,请参考以下文章

POJ2778DNA Sequence(AC自动机)

当 URL 包含片段时,iframe 会导致父元素在 Google Chrome 上向上滚动

错误 C2062:类型 int 意外

LeetCode 2062. 统计字符串中的元音子字符串

ural 2062 Ambitious Experiment

Hdoj 2062