http://acm.hdu.edu.cn/showproblem.php?pid=2062

Subset sequence

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3569    Accepted Submission(s): 1802

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
 
规律:
比如3个的,如果把每种深度视为一大格,那么对于任意深度d(start from 0),
*              第一个一定是空,剩下有n-d个没被使用的数字按照顺序排列,
1              且n-d个数字的小格高度相同
  *        明显,我们可以提前处理出这个高度
  2        第(n-1)层每个数字占1个高度
    *  第(n-2)层每个数字占2个高度,1个空格,1个数字
    3  第(n-3)层每个数字有5个高度,1个空格,2个数字
  3       ......
    *  第i层有:dis[i]=(n-1-i)*dis[i+1]+1
    2  所以 dis[i+1]=(dis[i]-1)/(n-1-i)
2             也就是dis[i]=(dis[i-1]-1)/(n-i)
  *       处理出来之后,一层层查找确定对应位上的数字即可
  1
    *
    3
  3
    *
    1
3
  *
  1
    *
    2
  2
    *
    1
 
#include <iostream>
#include <cstring>
using namespace std;
typedef unsigned long long ll;
int n;
ll m;
int bit[20],len;
bool used[21];
int fnd(int ind){
int ind2=0;
for(int i=1;i<21;i++){
if(!used[i]){
if(ind==ind2)return i;
ind2++;
}
}
return -1;
}
ll all;
ll dis[21];
int main(){
while(cin>>n>>m){
memset(used,0,sizeof(used));
all =1;
ll sub=1;
for(int i=0;i<n;i++){
sub*=(n-i);
all+=sub;
} for(int i=0;i<n;i++){
all--;
dis[i]=all/(n-i);
all/=(n-i);
}
len=0;
for(int i=n;i>=1;i--,len++){
if(m==0){break;}
m--;
bit[len]=fnd(m/dis[len]);
used[bit[len]]=true;
m%=dis[len];
}
for(int i=0;i<len;i++){
cout<<bit[i]<<(i==len-1?'\n':' ');
}
}
return 0;
}

  

HDU 2062 Subset sequence 数位dp,思路 难度:1的更多相关文章

  1. HDU 2062 Subset sequence (找规律)

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

  2. 题解报告:hdu 2062 Subset sequence

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

  3. HDU 2062 Subset sequence

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

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

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

  5. HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛

    普通的数位DP计算回文串个数 /* HDU 6156 - Palindrome Function [ 数位DP ] | 2017 中国大学生程序设计竞赛 - 网络选拔赛 2-36进制下回文串个数 */ ...

  6. hdu 5898 odd-even number 数位DP

    传送门:hdu 5898 odd-even number 思路:数位DP,套着数位DP的模板搞一发就可以了不过要注意前导0的处理,dp[pos][pre][status][ze] pos:当前处理的位 ...

  7. HDU 3555 Bomb(数位DP)

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Subm ...

  8. hdu 5106 Bits Problem(数位dp)

    题目链接:hdu 5106 Bits Problem 题目大意:给定n和r,要求算出[0,r)之间全部n-onebit数的和. 解题思路:数位dp,一个ct表示个数,dp表示和,然后就剩下普通的数位d ...

  9. [hdu 2089] 不要62 数位dp|dfs 入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求[n, m]区间内不含4和62的数字个数. 这题有两种思路,直接数位dp和dfs 数位d ...

随机推荐

  1. eclipse中Web Deployment Assembly与build path作用

    java Build path是编译路径设置,主要用来设置源代码的编译路径默认是default output folder Web Deployment Assembly是eclipse中的发布路径设 ...

  2. Storm概念讲解,工作原理

    一.storm是一个用于实时流式计算的分布式计算引擎,弥补了Hadoop在实时计算方面的不足(Hadoop在本质上是一个批处理系统). 二.storm在实际应用场景中的位置一般如下: 其中的编号1~5 ...

  3. 【虚拟机】linux 桥接模式 固定静态IP

    1:在root用户下修改 2:进入root用户下 vim/etc/sysconfig/network-scripts/ifcfg-eth0 2.修改完保存退出 3.重启网络服务:service net ...

  4. GIT使用—创建并使用远程版本库

    远程版本库 (1)创建一个裸版本库 [root@localhost tmp]# git init fluff2 Initialized empty Git repository in /tmp/flu ...

  5. 关于HttpRuntime.Cache的运用

    存Cache方法: HttpRuntime.Cache.Add( KeyName,//缓存名 KeyValue,//要缓存的对象 Dependencies,//依赖项 AbsoluteExpirati ...

  6. 八皇后问题解题报告(dfs

    这里是代码传送门 所谓八皇后问题,一开始接触,上学期舍友提及的,但是因为各种原因,水平不够,并没有关心,偶然之间,再次遇见,便进行的尝试(棋盘是0-7的,不是1-8的...开始打弄错了) 所谓八皇后问 ...

  7. Hive中排序和聚集

    //五种子句是有严格顺序的: where → group by → having → order by → limit ; //distinct关键字返回唯一不同的值(返回age和id均不相同的记录) ...

  8. JAVA基础补漏--可变参数

    使用场景:当参数列表数据类型已确定,但参数的个数不确定,就可以用可变参数 格式: 修饰符 返回值类型 方法名(数据类型...变量名){} 原理:可变参数底层为一数组,可根据变量个数的不同,创建不同长度 ...

  9. Import SQL into MySQL with a progress meter

    There is nice tool called pv # On Ubuntu/Debian system $ sudo apt-get install pv # On Redhat/CentOS ...

  10. 04_zookeeper客户端使用及常用命令

    zookeeper客户端的使用 (1)   首先找到zookeeper自带客户端的位置 简单来说,zookeeper自带客户端位于zookeeper安装目录的bin目录下,以我的为例: (2)   运 ...