题目链接

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

分析:

给定1,2,3...N个数的集合,现在求所有非空子集(相同元素不同位置视为不同)按字典序排序后的第m个集合是什么?

我们用f[i]来表示i个元素的子序列的个数,可以找出地推的规律:f[n] = n * (f[n-1] + 1);

思路:求n个元素时序列首元素,序列变为n-1,

求n-1个元素时序列首元素......

用t = ceil(m/(f[n-1]+1)),即可求得所求序列在所有序列中是第几组,也就是当前第一个元素在序列数组a中的位置

用数组a表示序列数组[1,2,...,n](需要动态更新,每次求出t之后,都要删除t位置的元素)

在更新之后,序列数组总长度变为n-1,我们要求一下所求序列的新位置m = m - (t-1)*(f[n-1]+1) - 1(前面有t-1组,每组f[n-1]个元素)

代码:

#include<stdio.h>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
long long m, f[25];
f[0] = 0;
for (int i =1; i < 21; i++)
f[i] = i * (f[i - 1] + 1);
while (scanf("%d%lld", &n, &m) != EOF)
{
int flag = 1, a[25];
for (int i = 1; i <= n; i++)
a[i] = i;
while (m > 0)
{
int t = ceil(m * 1.0 / (f[n - 1] + 1));//求出的是n个数字的首元素的位置
if (!flag)
printf(" ");
flag = 0;
printf("%d", a[t]);
for (int i = t; i < n; i++)//然后要删去这个元素
a[i] = a[i + 1];
m = m - (t - 1) * (f[n - 1] + 1) - 1;//去掉一个元素后,总共的个数也要改变
n--;
}
printf("\n");
}
return 0;
}

最新文章

  1. 使用spring过程中遇到的问题
  2. 006. C#使用WMI操作远程计算机
  3. nbIoT基础概念
  4. VS2012执行Cocos2d-xTest案例载入失败解决方式
  5. poj 3620 Avoid The Lakes【简单dfs】
  6. 伪造 UDP 包源 IP 地址
  7. iOS 使用Block实现函数回调
  8. 【LeetCode从零单排】No189 .Rotate Array
  9. 前端设计技巧——用 Promise 处理交互和异步
  10. 02 Java类的加载机制
  11. [51nod1239欧拉函数之和]
  12. 将已经存在的异步请求callback转换为同步promise
  13. 在CentOS上配置SAMBA共享目录(转载)
  14. PhoneGap &amp; Cordova 安装白皮书
  15. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)D. Frequency of String
  16. 自定义控件详解(三):Canvas效果变换
  17. 帝国cms建站-动态获取栏目id
  18. HDU 4511 小明系列故事——女友的考验 (AC自动机+DP)
  19. Flip Game---poj1753(状压+bfs)
  20. php 关联数组遍历

热门文章

  1. 虚拟机centos 安装 redis 环境 linux 使用 java 远程连接 redis
  2. 【Nginx笔记】 fastcgi_param解释
  3. Mxnet Windows配置
  4. NOIP赛前集训营-提高组(第一场)#B 数数字
  5. 【题解】HAOI2018染色
  6. 获取远程图片的Blob资源
  7. 【poj3016】 K-Monotonic
  8. 滴滴打车CTO张博:生死战役,技术和时间赛跑
  9. java多线程 -- ReadWriteLock 读写锁
  10. Android中Selector的用法(改变ListView和Button的默认背景)