程设---A decorative fence

Posted by Kevin on June 16, 2022

Description

Richard just finished building his new house. Now the only thing the house misses is a cute little wooden fence. He had no idea how to make a wooden fence, so he decided to order one. Somehow he got his hands on the ACME Fence Catalogue 2002, the ultimate resource on cute little wooden fences. After reading its preface he already knew, what makes a little wooden fence cute. A wooden fence consists of N wooden planks, placed vertically in a row next to each other. A fence looks cute if and only if the following conditions are met: �The planks have different lengths, namely 1, 2, . . . , N plank length units. �Each plank with two neighbors is either larger than each of its neighbors or smaller than each of them. (Note that this makes the top of the fence alternately rise and fall.) It follows, that we may uniquely describe each cute fence with N planks as a permutation a1, . . . , aN of the numbers 1, . . . ,N such that (any i; 1 < i < N) (ai − ai−1)*(ai − ai+1) > 0 and vice versa, each such permutation describes a cute fence. It is obvious, that there are many dierent cute wooden fences made of N planks. To bring some order into their catalogue, the sales manager of ACME decided to order them in the following way: Fence A (represented by the permutation a1, . . . , aN) is in the catalogue before fence B (represented by b1, . . . , bN) if and only if there exists such i, that (any j < i) aj = bj and (ai < bi). (Also to decide, which of the two fences is earlier in the catalogue, take their corresponding permutations, find the first place on which they differ and compare the values on this place.) All the cute fences with N planks are numbered (starting from 1) in the order they appear in the catalogue. This number is called their catalogue number. img After carefully examining all the cute little wooden fences, Richard decided to order some of them. For each of them he noted the number of its planks and its catalogue number. Later, as he met his friends, he wanted to show them the fences he ordered, but he lost the catalogue somewhere. The only thing he has got are his notes. Please help him find out, how will his fences look like.

Input

The first line of the input file contains the number K (1 <= K <= 100) of input data sets. K lines follow, each of them describes one input data set. Each of the following K lines contains two integers N and C (1 <= N <= 20), separated by a space. N is the number of planks in the fence, C is the catalogue number of the fence. You may assume, that the total number of cute little wooden fences with 20 planks fits into a 64-bit signed integer variable (long long in C/C++, int64 in FreePascal). You may also assume that the input is correct, in particular that C is at least 1 and it doesn抰 exceed the number of cute fences with N planks.

Output

For each input data set output one line, describing the C-th fence with N planks in the catalogue. More precisely, if the fence is described by the permutation a1, . . . , aN, then the corresponding line of the output file should contain the numbers ai (in the correct order), separated by single spaces.

Sample Input

1
2
3
2
2 1
3 3

Sample Output

1
2
1 2
2 3 1

solution

题意:求N的交错排列的第C个

记 $up[i][j]$ 表示 $1 \sim i$ 的交错排列,$j$ 开头并且 $j$ 的下一个比 $j$ 大的方案数(这里 $j \leqslant i$ )

记 $down[i][j]$ 表示 $1 \sim i$ 的交错排列,$j$ 开头并且 $j$ 的下一个比 $j$ 小的方案数

$down[i][j]=\sum_{k=1}^{j-1} up[i-1][k]$

$up[i][j]=down[i][i-j+1]$

第二个式子是因为把一个排列中的 $x$ 换成 $n-x+1$ 是双射

接下来它要求第 c 个排列是什么

假设首先 $down[n][i]$ 必然排在 $up[n][i]$ 的前面

枚举 $i$ ,若 $down[n][i]\geqslant c$ 则说明第一位必然是 $ i $ ,且为下降

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#define ll long long
using namespace std;
int T,n;
ll up[22][22],down[22][22],c;
int flag[22],ans[22];
void dp(){
	n=20;
	up[1][1]=down[1][1]=1;
	for(int i=1;i<=n;i++){
		for(int j=2;j<=i;j++){
			for(int k=1;k<j;k++)down[i][j]+=up[i-1][k];
		}
		for(int j=1;j<=i;j++)up[i][j]=down[i][i-j+1];
		
	}
}
void work(){
	scanf("%d%lld",&n,&c);
	for(int i=1;i<=n;i++)flag[i]=1;
	bool f;
	for(int j=1;j<=n;j++){
		if(down[n][j]>=c){
			ans[n]=j;f=0;
			flag[j]=0;
			break;
		}
		c-=down[n][j];
		if(up[n][j]>=c){
			ans[n]=j;f=1;
			flag[j]=0;
			break;
		}
		c-=up[n][j];
	}
//	cout<<ans[n]<<' '<<f<<endl;
	for(int i=n-1;i>=1;i--){
		int k=0;
		if(!f){
			for(int j=1;j<=n;j++){
				if(!flag[j])continue;
				k++;
				if(j<ans[i+1]){
					if(up[i][k]>=c){
						flag[j]=0;
						ans[i]=j;f=1;break;
					}
					c-=up[i][k];
				}
			}
		}
		else {
			for(int j=1;j<=n;j++){
				if(!flag[j])continue;
				k++;
				if(j>ans[i+1]){
					if(down[i][k]>=c){
						flag[j]=0;
						ans[i]=j;f=0;break;
					}
					c-=down[i][k];
				}
			}
		}
	}
	for(int i=n;i>=1;i--)printf("%d ",ans[i]);puts("");
	
} 
int main(){
	scanf("%d",&T);
	dp();
	while(T--)work();
	return 0;
}