POJ1037: A decorative fence (DP)

Source: Internet
Author: User
Tags bitset

POJ1037: A decorative fence (DP)

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 catalogger 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 using 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.

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 in C/C ++, int64 in FreePascal ). you may also assume that the input is correct, in particle that C is at least 1 and it doesn has 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 shoshould contain the numbers ai (in the correct order), separated by single spaces.

Sample Input

22 13 3

Sample Output

1 22 3 1

Source

CEOI 2002
Question: 1 ~ N of n wood sticks, what is the m-TH series in full wave-shaped arrangement?
# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             Using namespace std; # define LS 2 * I # define RS 2 * I + 1 # define UP (I, x, y) for (I = x; I <= y; I ++) # define DOWN (I, x, y) for (I = x; I> = y; I --) # define MEM (a, x) memset (, x, sizeof (a) # define W (a) while () # define LL long # define N 25 # define MOD 19999997 # define INF 0x3f3f3f # define EXP 1e-8const double Pi = acos (-1.0); int t, n; LL m; LL dp [N] [N] [2]; // 0-UP, 1-DOWN // dp [I] [k] [1] is the number of DOWN schemes for hitting the head of the wood rod with the nth length, C [I] [k] [0] is the number of UP schemes for hitting the wooden rod with the nth k length, The k short refers to the k short int a [N], ans [N], vis [N]; void solve () {int I, j, k, cnt; LL now = 0, pre; // number of enumerated solutions MEM (vis, 0); MEM (ans, 0); UP (I, 1, n) {cnt = 0; UP (j, 1, n) // enumerate all sticks {pre = now; if (! Vis [j]) {cnt ++; if (I = 1) now + = dp [n] [cnt] [0] + dp [n] [cnt] [1]; else if (j> ans [I-1] & (I = 2 | ans [I-2]> ans [I-1]) now + = dp [n-I + 1] [cnt] [1]; else if (j = m) break; // The total number of all subsequent numbers is more than m. We can confirm that this number is j} vis [j] = 1; ans [I] = j; now = pre;} printf ("% d", ans [1]); UP (I, 2, n) printf ("% d", ans [I]); printf ("\ n");} int main () {int I, j, k; MEM (dp, 0 ); dp [1] [1] [0] = dp [1] [1] [1] = 1; UP (I, 2, 20) {UP (j, 1, I) // enumerate the length of the first stick {// the length of the second stick UP (k, j, I-1) dp [I] [j] [0] + = dp [I-1] [k] [1]; UP (k, 1, J-1) dp [I] [j] [1] + = dp [I-1] [k] [0];} scanf ("% d", & t ); W (t --) {scanf ("% d % I64d", & n, & m); solve () ;}return 0 ;}
           
          
         
        
       
     
    
   
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.