CODEFORCES Rockethon 2015 B. Permutations, rockethon2015

Source: Internet
Author: User

CODEFORCES Rockethon 2015 B. Permutations, rockethon2015

You are given a permutation p of numbers 1, clerk 2, clerk ,..., Limit n. Let's define f (p) as the following sum:

Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f (p ).
Input

The single line of input contains two integers n and m (1 ≤ medium m ≤ medium cntn ), where cntn is the number of permutations of length n with maximum possible value of f (p ).

The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.

In subproblem B1 (3 points), the constraint 1 ≤ n ≤ 8 will hold.In subproblem B2 (4 points), the constraint 1 ≤ n ≤ 50 will hold. 

Output

Output n number forming the required permutation.
Sample test (s)
Input

2 2

Output

2 1

Input

3 2

Output

1 3 2

Note

In the first example, both permutations of numbers {1, 2} yield maximum possible f (p) which is equal to 4. among them, (2, interval 1) comes second in lexicographical order.

Let's first give us an f (p), and find the lexicographic orders in the largest f (p) of n numbers that are m.

We can find that
F (1) = 1
F (2) = 2
F (3) = 4
F (4) = 8
F (5) = 16
....
So f (n) = (2 ^ (n-1 ))
And in these arrangements, 1 must be at 1st or the last, 2 must be at 2nd or the second to the last ..... Based on these, we can recursively find the arrangement

/*************************************** * *********************************> File Name: b. cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: monday, February 09, 2015 ******************************** **************************************** /# include <map> # include <set> # include <queue> # include <stack> # include <vector> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstring> # include <iostream> # include <algorithm> using namespace std; const double pi = acos (-1); const int inf = 0x3f3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair <int, int> PLL; int n, cnt; LL m; int ans [55]; void work (int l, int r) {if (l = r) {ans [l] = cnt ++; return;} LL tmp = (1LL <(r-l-1); if (tmp> = m) {ans [l] = cnt ++; work (l + 1, r) ;}else {ans [r] = cnt ++; m-= tmp; work (l, r-1 );}} int main () {while (cin> n> m) {cnt = 1; work (1, n); printf ("% d", ans [1]); for (int I = 2; I <= n; ++ I) {printf ("% d", ans [I]);} printf ("\ n ");} return 0 ;}

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.