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

Sample Output

1
1
1 2
2
2 1
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代码

/*
* @Author: WZY
* @School: HPU
* @Date: 2018-11-11 15:02:40
* @Last Modified by: WZY
* @Last Modified time: 2018-11-11 16:06:05
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <time.h>
#define ll long long
#define ull unsigned long long
#define ms(a,b) memset(a,b,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
#define bug cout<<"---------"<<endl
#define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"\n"
const double E=exp(1);
const ll maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
// F[n]=(F[n-1]+1)*n
// 计算n对应的子集的个数
ll F(ll n)
{
if(n<=1)
return n;
return (F(n-1)+1)*n;
}
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
double _begin_time = clock();
#endif
ll n,m;
ll a[30];
while(cin>>n>>m)
{
for(int i=1;i<30;i++)
a[i]=i;
int flag=0;
while(m>0)
{
ll k=F(n-1)+1; //记录每行有多少个集合
int res=m/k+1; //记录在第几行
if(m%k==0) //在上一行
res--;
if(flag)
cout<<" ";
cout<<a[res];
for(int i=res;i<=n;i++)
a[i]=a[i+1];
m-=k*(res-1)+1;
n--;
flag++;
}
cout<<endl;
}
#ifndef ONLINE_JUDGE
double _end_time = clock();
printf("time = %lf ms.", _end_time - _begin_time);
#endif
return 0;
}

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. ubuntu16.04安装wordpress

    ubuntu16.04安装wordpress和centos7安装wordpress存在一定的差异. 当然共性大于差异. 共性是lamp环境. wordpress的必备环境. 先共性再差异. 一.搭建l ...

  2. 在数据库级别还是在service层进行级联删除

    在数据库配置级联删除的话,父表删除子表也删除.但是应该将维护代码放在一处,不要在service上删除父表,而在数据库层面级联删除子表,应该都在service层上进行删除.

  3. JavaScript-DOM(1)

    DOM简介 DOM 节点分类 DOM 节点层级关系 1.文档节点 1.父节点 2.标签(元素)节点 2.子节点 3.属性节点 3.兄弟节点 4.注释节点 4.根节点 5.文本节点 DOM节点分类 DO ...

  4. 剑指offer 02:替换空格

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 解题代码 public cla ...

  5. Monent.js:强大的日期处理类库

    一.介绍及安装 1.1 介绍 Moment.js是一个优秀的JavaScript 日期处理类库. 如果没有Moment.js之类的日期处理库,我们如果需要获得格式化后的日期.往往需要通过new Dat ...

  6. 3、VNC

    VNC(Virtual Network Computing,虚拟网络计算机) VNC分为两部分组成:VNC server 和 VNC viewer VNC安装 1.yum install tigerv ...

  7. BFS GPLT L2-016 愿天下有情人都是失散多年的兄妹

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805061769609216 分析:一开始以为是并查集..... ...

  8. Java基础恶补——内存泄露、内存溢出

    http://blog.csdn.net/wisgood/article/details/16818243

  9. 详解Oracle partition分区表

    随着表中行数的增多,管理和性能性能影响也将随之增加.备份将要花费更多时间,恢复也将 要花费更说的时间,对整个数据表的查询也将花费更多时间.通过把一个表中的行分为几个部分,可以减少大型表的管理和性能问题 ...

  10. analyse web.xml of hello1

    web.xml注释分析: 补充: 一.XML文档的xmlns.xmlns:xsi和xsi:schemaLocation (参考博客:https://www.cnblogs.com/osttwz/p/6 ...