hduoj 2062Subset sequence

Subset sequence

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

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

Author

LL

Source

校庆杯Warm
Up

Recommend

linle   |   We have carefully selected several similar
problems for you:  2059 2065 2056 2058 2061

学习网址:http://blog.csdn.net/lianqi15571/article/details/8877014

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<stack>
 5 #include<set>
 6 #include<map>
 7 #include<queue>
 8 #include<algorithm>
 9 using namespace std;
10 long long c[21]={0};//c[n]表示长度为n,第一位固定,剩下数字排列形成数列的个数
11 //易知c[n]=(n-1)*c[n-1]+1
12 //含义:比如第一位是A1  剩下A2A3。。An 共n-1个数可以固定在第二位,再加上一个空集
13 bool s[22];
14 int main(){
15     //freopen("D:\\INPUT.txt","r",stdin);
16     int n,i,j;
17     long long m;
18     for(i=1;i<21;i++){
19         c[i]=(i-1)*c[i-1]+1;
20         //cout<<c[i]<<endl;
21     }
22     long long t,count;
23     while(scanf("%d %lld",&n,&m)!=EOF){
24         memset(s,false,sizeof(s));
25         j=n;
26         queue<int> q;
27         while(m){
28           count=0;
29           t=m/c[j]-(m%c[j]==0?1:0);
30           m-=t*c[j];
31           m--;//去掉一个空集!!
32           j--;
33           for(i=1;i<=n;i++){
34             if(!s[i]){
35                 count++;
36                 if(count==t+1){
37                     break;
38                 }
39             }
40           }
41           s[i]=true;
42           q.push(i);
43         }
44         printf("%d",q.front());
45         q.pop();
46         while(!q.empty()){
47             printf(" %d",q.front());
48             q.pop();
49         }
50         printf("\n");
51     }
52     return 0;
53 }
时间: 2024-10-03 22:49:14

hduoj 2062Subset sequence的相关文章

HDUOJ Number Sequence 题目1005

 /*Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 127146    Accepted Submission(s): 30901 Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n

HDUOJ 1005 Number Sequence

直接用递归模拟会超内存,考虑到对7取余,A,B为定值,所以可以进行打表 1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 int main() 5 { 6 int a, b, n, i, t; 7 int f[50]; 8 f[1] = f[2] = 1; 9 while((3 == scanf("%d%d%d", &a, &b, &n)), a ||

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

HDUOJ P1702 ACboy needs your help again!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8599    Accepted Submission(s): 4306 Problem Description ACboy was kidnapped!! he miss his mother very much and is very scare now.You can't image

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

HDU 5783 Divide the Sequence(数列划分)

HDU 5783 Divide the Sequence(数列划分) Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem Description - 题目描述 Alice has a sequence A, She wants to split A into as much as possible continuous subsequences, satisfy

HDU 3397 Sequence operation(线段树)

HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变成1,1变成0 3 a b 查询[a,b]区间1的个数 4 a b 查询[a,b]区间连续1最长的长度 思路:线段树线段合并.须要两个延迟标记一个置为01,一个翻转,然后因为4操作,须要记录左边最长0.1.右边最长0.1,区间最长0.1,然后区间合并去搞就可以 代码: #include <cstdi

HDU 3998 Sequence (最长递增子序列+最大流SAP,拆点法)经典

Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1666    Accepted Submission(s): 614 Problem Description There is a sequence X (i.e. x[1], x[2], ..., x[n]). We define increasing subsequ

HDOJ 5147 Sequence II 树状数组

树状数组: 维护每一个数前面比它小的数的个数,和这个数后面比他大的数的个数 再枚举每个位置组合一下 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 121    Accepted Submission(s): 58 Problem Description Long long ago, there is a seq