2013
11-08

# A decorative fence

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 di erent 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.

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.

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.

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.

2
2 1
3 3

1 2
2 3 1

//* @author:
import java.util.*;
public class Main{
static final int MAX=21;//最多20根木条
static long w[][];//w型排列的个数
static long m[][];//m型排列的个数，m[x][n]为由n个长度不同的木条组成的栅栏中，以其中第x根木条开始的“M”型排列的个数

private int n;//木条的根数
private long c;//编号

private boolean flag[];
private boolean print;

static {//初始化静态变量
w=new long[MAX][MAX];
m=new long[MAX][MAX];
w[1][1] = 1; m[1][1] = 1;
w[1][2] = 0; m[1][2] = 1;
w[2][2] = 1; m[2][2] = 0;
for (int i = 3; i < MAX; i++)
for (int j = 1; j < MAX; j++){
for (int k = 1; k < j; k++)
w[j][i] += m[k][i - 1];
for (int k = j; k < i; k++)
m[j][i] += w[k][i - 1];
}
}

public Main(int n,long c){
this.n=n;
this.c=c;
flag=new boolean[MAX];
print=false;
}

private void fand(int k, int m){
for (int i = 1; i <= m; i++)
if (flag[i] && i <= k){
k++; m++;
}
flag[k] = true;
if (print) System.out.print(" "+k);
else{
System.out.print(k);
print = true;
}
}

private void doIt(){
int k = 1;//从排在最前面的木条找起
print = false;
boolean direct = true, first = true;
while(n!=0){
if (direct)
if (c > w[k][n]){//如果编号大于以第k根木条开始的“w”型排列的个数，说明所求排列在这些排列之后
c -= w[k++][n];//更新编号，同时换一根木条
if (first) {direct = false; k--;}//如果是查找第一根木条，还要试"m"型排列
}else{
fand(k, n--);//找到k,输出并将木条总数减1
first = false;
direct = false;
k = 1;
}
else
if (c > m[k][n]){//如果编号大于以第k根木条开始的“m”型排列的个数，说明所求排列在这些排列之后
c -= m[k++][n];//更新编号，同时换一根木条
if (first) direct = true;//查找第一根木条结束
}else{
fand(k, n--);
first = false;
direct = true;
}
}
System.out.println();
}

public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int t, n;
long c;
t=sc.nextInt();;
while((t--)!=0){
n=sc.nextInt();
c=sc.nextLong();
Main m=new Main(n,c);
m.doIt();
}
}

}

1. bottes vernies blanches

I appreciate the efforts you men and women place in to share blogs on such sort of matters, it was certainly useful. Keep Posting!