Subset sequence

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 8188 Accepted Submission(s): 3735

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 1
  2. 2 1
  3. 2 2
  4. 2 3
  5. 2 4
  6. 3 10

Sample Output

  1. 1
  2. 1
  3. 1 2
  4. 2
  5. 2 1
  6. 2 3 1

题意

对于一个序列An={1,2,3...n}A_n=\{1,2,3...n\}An​={1,2,3...n},A1={1},A3={1,2,3}A_1=\{1\},A_3=\{1,2,3\}A1​={1},A3​={1,2,3},我们称一个非空子集元素的排列为一个子集序列。对所有的子集序列按字典顺序排序。求出第m个子序列

思路

首先要求出AnA_nAn​有多少子集:

n=1n=1n=1时,子集个数为1个

n=2n=2n=2时,子集个数为4个

n=3n=3n=3时,子集个数为15个

子集的个数满足F(n)=(F(n−1)+1)×nF(n)=(F(n-1)+1)\times nF(n)=(F(n−1)+1)×n

以n=3n=3n=3为例,将AnA_nAn​全部列出来可得:

{1},{1,2},{1,2,3},{1,3},{1,3,2}\{1\},\{1,2\},\{1,2,3\},\{1,3\},\{1,3,2\}{1},{1,2},{1,2,3},{1,3},{1,3,2}

{2},{2,1},{2,1,3},{2,3},{2,3,1}\{2\},\{2,1\},\{2,1,3\},\{2,3\},\{2,3,1\}{2},{2,1},{2,1,3},{2,3},{2,3,1}

{3},{3,1},{3,1,2},{3,2},{3,2,1}\{3\},\{3,1\},\{3,1,2\},\{3,2\},\{3,2,1\}{3},{3,1},{3,1,2},{3,2},{3,2,1}

例如:

在计算样例3 10时,先计算第10个子集位于第几行(以哪个数字开头)。输出该数字后,把该数字从AnA_nAn​中删除,然后删掉第二行之前的所有子集,和第二行的第一个子集{2}\{2\}{2},mmm变成在剩余集合中的子集的位置。重复操作,至到mmm小于0为止

AC代码

  1. /*
  2. * @Author: WZY
  3. * @School: HPU
  4. * @Date: 2018-11-11 15:02:40
  5. * @Last Modified by: WZY
  6. * @Last Modified time: 2018-11-11 16:06:05
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <iostream>
  11. #include <algorithm>
  12. #include <math.h>
  13. #include <limits.h>
  14. #include <map>
  15. #include <stack>
  16. #include <queue>
  17. #include <vector>
  18. #include <set>
  19. #include <string>
  20. #include <time.h>
  21. #define ll long long
  22. #define ull unsigned long long
  23. #define ms(a,b) memset(a,b,sizeof(a))
  24. #define pi acos(-1.0)
  25. #define INF 0x7f7f7f7f
  26. #define lson o<<1
  27. #define rson o<<1|1
  28. #define bug cout<<"---------"<<endl
  29. #define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
  30. const double E=exp(1);
  31. const ll maxn=1e6+10;
  32. const int mod=1e9+7;
  33. using namespace std;
  34. // F[n]=(F[n-1]+1)*n
  35. // 计算n对应的子集的个数
  36. ll F(ll n)
  37. {
  38. if(n<=1)
  39. return n;
  40. return (F(n-1)+1)*n;
  41. }
  42. int main(int argc, char const *argv[])
  43. {
  44. ios::sync_with_stdio(false);
  45. #ifndef ONLINE_JUDGE
  46. freopen("in.txt", "r", stdin);
  47. freopen("out.txt", "w", stdout);
  48. double _begin_time = clock();
  49. #endif
  50. ll n,m;
  51. ll a[30];
  52. while(cin>>n>>m)
  53. {
  54. for(int i=1;i<30;i++)
  55. a[i]=i;
  56. int flag=0;
  57. while(m>0)
  58. {
  59. ll k=F(n-1)+1; //记录每行有多少个集合
  60. int res=m/k+1; //记录在第几行
  61. if(m%k==0) //在上一行
  62. res--;
  63. if(flag)
  64. cout<<" ";
  65. cout<<a[res];
  66. for(int i=res;i<=n;i++)
  67. a[i]=a[i+1];
  68. m-=k*(res-1)+1;
  69. n--;
  70. flag++;
  71. }
  72. cout<<endl;
  73. }
  74. #ifndef ONLINE_JUDGE
  75. double _end_time = clock();
  76. printf("time = %lf ms.", _end_time - _begin_time);
  77. #endif
  78. return 0;
  79. }

HDU 2062:Subset sequence(思维)的更多相关文章

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

    http://acm.hdu.edu.cn/showproblem.php?pid=2062 Subset sequence Time Limit: 1000/1000 MS (Java/Others ...

  2. HDU 2062 Subset sequence (找规律)

    题目链接 Problem Description Consider the aggregate An= { 1, 2, -, n }. For example, A1={1}, A3={1,2,3}. ...

  3. 题解报告:hdu 2062 Subset sequence

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2062 Problem Description 考虑集合An = {1,2,...,n}. 例如,A1 ...

  4. HDU 2062 Subset sequence

    我是把它当做一道数学题来做的. 这篇题解写的有点啰嗦,但是是我最原始的思维过程. 对于一个集合An= { 1, 2, …, n },在n比较小的情况下,在纸上按字典顺序把所有子集排列一下. 以n=3, ...

  5. hdu(2062)-Subset sequence 组合数学

    意甲冠军:查找集合{1,2,3...n}第一m一个排列子. 收集的线索所行的大小. 例两个元素的排列子集合按字典树排列是:{1},{1,2},{2},{2,1}: 解法:一个一个元素来确定,每次把剩余 ...

  6. 【HDOJ】2062 Subset sequence

    这道题目非常好,饶了点儿圈子.我的思路是,先按照组排列.例如,1            2           31 2         2 1        3 11 2 3      2 1 3  ...

  7. HDU 5860 Death Sequence(死亡序列)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  8. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  9. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

随机推荐

  1. Java 泛型方法、泛型类、通配符、通配符上下限

    泛型方法 泛型方法定义规则: 所有泛型方法声明都有一个类型参数声明部分(由尖括号分隔),该类型参数声明部分在方法返回类型之前. 每一个类型参数声明部分包含一个或多个类型参数,参数间用逗号隔开.一个泛型 ...

  2. Mac redis安装

    Download, extract and compile Redis with: #进入下载目录 $ cd ... $ wget http://download.redis.io/releases/ ...

  3. DAY8 文件操作(二)

    一.写 1.1写文件 # w:没有文件新建文件,有文件就清空文件 w = open('1.txt', 'w', encoding='utf-8') w.write('000\n') # 在写入大量数据 ...

  4. 函数式语言简介(functional language)

    1.什么是函数式语言?        是一种非冯·诺伊曼式的程序设计语言.函数式语言主要成分是原始函数.定义函数和函数型.这种语言具有较强的组织数据结构的能力,可以把某一数据结构(如数组)作为单一值处 ...

  5. mockjs学习

    mockjs简单学习与应用,可以满足工作所需就行.*************************************************************************** ...

  6. Android24以上拍照代码

    public void takePic(){ //创建File对象,用于存储拍照后的照片 File outputImage = new File(getExternalCacheDir()," ...

  7. 批量注册当前文件夹中的dll和ocx

    新建文件:RegisterDllAndOcx.bat如下 @echo offecho hello,girl~~for %%i in (*.dll *.ocx) do (echo %% register ...

  8. 2015-10-13 jQuery5实例

    jQuery(5)   一.扑克牌切换 <body> <div class="ig ig1"><img src="image/1.jpg&q ...

  9. python avro 数据格式使用demo

    {"name": "UEProcedures", "type": "record", "fields" ...

  10. trap(陷井)

    if True: x = 15 print(x)print(x) # 可见 if 语句,不是一个代码块,因为代码块有独立的作用域,代码块结束时,会释放变量 l1 = [1,2,3,4]print(id ...