CODEFORCES Rockethon 2015 B. Permutations

You are given a permutation p of numbers 1,?2,?…,?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?≤?m?≤?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,?1) comes second in lexicographical order.

先给我们一个f(p), 求出n个数中f(p)最大的那些排列里面字典序是m的那个排列

可以发现

f(1) =1

f(2) = 2

f(3) = 4

f(4) = 8

f(5) = 16

….

所以f(n) = (2 ^ (n - 1))

而且在这些排列中,1一定在第1或者最后,2一定在第2或者倒数第二 ….. ,根据这些,我们可以递归求出排列

/*************************************************************************
    > File Name: B.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年02月09日 星期一 14时42分08秒
 ************************************************************************/

#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 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;
}
时间: 2024-10-09 08:48:41

CODEFORCES Rockethon 2015 B. Permutations的相关文章

codeforces Rockethon 2015

比赛链接:http://codeforces.com/contest/513 A. Game time limit per test 2 seconds memory limit per test 256 megabytes Two players play a simple game. Each player is provided with a box with balls. First player's box contains exactly n1 balls and second pl

Codeforces Rockethon 2015 C

Problem N个公司在拍卖标价一件商品,每个公司会在各自的[Li,Ri]区间内等概率选取整数出价.出最高价的公司会赢得商品,若有多个公司出最高价,随机一个公司赢得商品.但是,有一条特殊规矩,赢得商品的公司所需付的钱不是它所标价的钱,而是除去它以外的其它公司所标价的钱的最大值.现求,赢得的公司所需付钱的期望. Limits TimeLimit(ms):2000 MemoryLimit(MB):256 N∈[2,5] Li,Ri∈[1,10000] Look up Original Proble

codeforces A. Slightly Decreasing Permutations 题解

Permutation p is an ordered set of integers p1,??p2,??...,??pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation

Codeforces Round Rockethon 2015

A. Game 题目大意:A有N1个球,B有N2个球,A每次可以扔1-K1个球,B每次可以扔1-K2个球,谁先不能操作谁熟 思路:.....显然每次扔一个球最优.... 1 #include<iostream> 2 #include<cstdio> 3 #include <math.h> 4 #include<algorithm> 5 #include<string.h> 6 #include<queue> 7 #define MOD

Rockethon 2015

A Game题意:A,B各自拥有两堆石子,数目分别为n1, n2,每次至少取1个,最多分别取k1,k2个, A先取,最后谁会赢. 分析:显然每次取一个是最优的,n1 > n2时,先手赢. 代码: 1 #include <bits/stdc++.h> 2 #define pb push_back 3 #define mp make_pair 4 #define esp 1e-14 5 #define lson l, m, rt<<1 6 #define rson m+1, r,

codeforces 340E Iahub and Permutations(错排or容斥)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Iahub and Permutations Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more import

Codeforces 463D Gargari and Permutations(求k个序列的LCS)

题目链接:http://codeforces.com/problemset/problem/463/D 题目大意:给你k个序列(2=<k<=5),每个序列的长度为n(1<=n<=1000),每个序列中的数字分别为1~n,求着k个序列的最长公共子序列是多长?解题思路:由于每个序列的数字分别为1~n即各不相同,所以可以用pos[i][j]记录第i个序列中j的位置.设dp[i]表示以i结尾的最长公共子序列长度,那么我们可以按顺序遍历第一个序列的位置i,再在第一个序列中枚举位置j(j<

Codeforces 987E Petr and Permutations(数组的置换与复原 、结论)

题目连接: Petr and Permutations 题意:给出一个1到n的序列,Petr打乱了3n次,Um_nik打乱了7n+1次,现在给出被打乱后的序列,求是谁打乱的. 题解:因为给出了一个3*n和一个7*n+1,发现这两个当一个为奇数另一个一定为偶数,所以可以联想和奇偶性质有关.但是这里面要算最短几步能把当前的序列变成1-n.这里我算错~~顺便学了一下如何将置换序列复原. #include<bits/stdc++.h> using namespace std; typedef pair

CodeForces 340E Iahub and Permutations 错排dp

Iahub and Permutations 题解: 令 cnt1 为可以没有限制位的填充数字个数. 令 cnt2 为有限制位的填充数字个数. 那么:对于cnt1来说, 他的值是cnt1! 然后我们对cnt2进行dp. 对于任意一个新加进来的数字,我们可以令一个一个没有限制位数放在这里, 那么新加进来的数字 ≍ 没有限制位, 他的方案为 i-1 * dp[i-1] , 然后我们如果把这个数字放到有限制位的数来说, 那么他的转移方程就和错排一样了. 代码: #include<bits/stdc++