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调整字体大小

    window->preferences->general->appearance->colors and fonts-> 双击Text Font 就调整字体大小了

  2. java容器的数据结构-ArrayList,LinkList,HashMap

    ArrayList: 初始容量为10,底层实现是一个数组,Object[] elementData 自动扩容机制,当添加一个元素时,数组长度超过了elementData.leng,则会按照1.5倍进行 ...

  3. armv7 armv7s arm64 i386 x86_64

    开发SDK的同学需要了解这些指令集代表什么. armv7|armv7s|arm64都是ARM处理器的指令集 i386|x86_64 是Mac处理器的指令集 arm64:iPhone7 | iphone ...

  4. iOS 动态调用方法

      - (void)bugly { dispatch_async(dispatch_get_global_queue(0, 0), ^{ if (NSClassFromString(@"Bu ...

  5. hibernate的面向对象查询的效率有多低?

    前言 老平台的查询速度很慢,需要进行优化(...说白了就是优化sql语句),老平台用的strus2+hibernate框架,查询基本都是使用的HSQL. 关于hsql HQL是Hibernate Qu ...

  6. [HAOI2017模拟]囚人的旋律

    没有传送门辣. 神奇的DP题. 首先看到这道题第一眼应该想到正解不是在图上搞,肯定要把原图转化成序列. 根据逆序对的性质.每个点和标号大于他的点连边的点,其权值必定要小于该点,而没和他连边的且标号大于 ...

  7. LeetCode (262):Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  8. 解读:MR多路径输入

    对于在一个MR-Job中使用多路径作为输入文件,一般有三种方法: 1).多次调用,加载不同路径: import org.apache.hadoop.mapreduce.lib.input.FileIn ...

  9. Swift学习笔记 - Swift属性只读

    在OC中我们经常用到只读属性,用readonly修饰一下就行了,但在Swift中已经不是这样修饰的了,下面记录一下Swift中只读属性的使用 在OC中的只读: //只读属性 @property(rea ...

  10. Mysql CASE WHEN 用法

    select sum(1) as col_0_0_, sum(case vciinfo.useable when -1 then 1 else 0 end) as col_1_0_, sum(case ...