hdu 2062 Subset sequence【有點康拓展開的意思】,hdusubset

來源:互聯網
上載者:User

hdu 2062 Subset sequence【有點康拓展開的意思】,hdusubset
Subset sequenceTime Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3441    Accepted Submission(s): 1740


Problem DescriptionConsider 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.
 
InputThe 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 ). 
OutputFor 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 12 3 1
題意:給出n和m,然後有很多個集合{1,2,,,,n}的非空子集,按照一定方式排列,例如n==3時,11 21 2 31 31 3 222 12 1 32 32 3 133 13 1 23 23 2 1然後輸出第m個序列。結題思路,仔細觀察可以看出,1、2、3開頭的集合個數是一樣多的,因此那m/(每組個數)可以得出第一個數字,然後依次類推即可
#include<cstdio>#include<iostream>#define LL long long intusing 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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.