Problem Description

A sequence of length n is called a permutation if and only if it's composed of the first n positive integers and each number appears exactly once.

Here we define the "difference sequence" of a permutation p1,p2,…,pn as p2−p1,p3−p2,…,pn−pn−1. In other words, the length of the difference sequence is n−1 and the i-th term is pi+1−pi

Now, you are given two integers N,K. Please find the permutation with length N such that the difference sequence of which is the K-th lexicographically smallest among all difference sequences of all permutations of length N.

Input

The first line contains one integer T indicating that there are T tests.

Each test consists of two integers N,K in a single line.

* 1≤T≤40

* 2≤N≤20

* 1≤K≤min(104,N!)

Output

For each test, please output N integers in a single line. Those N integers represent a permutation of 1 to N, and its difference sequence is the K-th lexicographically smallest.

Sample Input

7
3 1
3 2
3 3
3 4
3 5
3 6
20 10000

Sample Output

3 1 2
3 2 1
2 1 3
2 3 1
1 2 3
1 3 2
20 1 2 3 4 5 6 7 8 9 10 11 13 19 18 14 16 15 17 12

题意:t 组数据,每组给出 n、k 两个数,定义差异排列为 ,求 n 的全排列,求出 n 的所有全排列中差异序列字典序第 k 小的排列

思路:

k 的范围是 1 到 min(1E4,n!),而 n 最大是 20,当 n=8 时,n!=40320,因此 k 最大是 1E4

故而对 n 分情况讨论:

  • n<=8 时:直接求出所有差异序列的全排列,然后 sort 排序后输出即可
  • n>=9 时:第一位直接取 n,然后再取剩下的 n-1 位的全排列,到 k 为止即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<unordered_map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 998244353;
const int N = 10000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;struct Node {int pos[22], p[22];
} node[50005];
int n,k;
bool cmp(Node x, Node y) {for (int i = 1; i < n; i++) {if (x.p[i] != y.p[i])return x.p[i] < y.p[i];}
}
int a[25];
int main() {int t;scanf("%d", &t);while (t--) {scanf("%d%d", &n, &k);if (n <= 8) { // n<=8时for (int i = 1; i <= n; i++)a[i] = i;for (int i = 1; i <= n; i++)//记录位置node[1].pos[i] = a[i];for (int i = 2; i <= n; i++)//求差异排列node[1].p[i - 1] = a[i] - a[i - 1];int num = 2;while (next_permutation(a + 1, a + 1 + n)) {//生成全排列for (int i = 1; i <= n; i++)node[num].pos[i] = a[i];for (int i = 2; i <= n; i++)node[num].p[i - 1] = a[i] - a[i - 1];num++;}sort(node + 1, node + num, cmp);for (int i = 1; i <= n - 1; i++)printf("%d ", node[k].pos[i]);printf("%d\n", node[k].pos[n]);} else { // n>=9时printf("%d ",n); //第一位一定是nfor(int i=1;i<=n-1;i++) //剩余的n-1位a[i]=i;if (k == 1) { //特判k=1for(int i=1;i<=n-2;i++)printf("%d ",a[i]);printf("%d\n",a[n-1]);}else{int num = 1;while (next_permutation(a + 1, a + 1 + (n - 1))) { //输出前k个num++;if (num == k) {for (int i = 1; i <= n - 2; i++)printf("%d ", a[i]);printf("%d\n", a[n - 1]);break;}}}}}return 0;
}

permutation 1(HDU-6628)相关推荐

  1. 有源汇有上下界最大流/最小流 配题(HDU 3157)

    因为是有源汇所以设源点为 s,汇点为 t. 有源汇有上下界最大流: 连接一条 t 指向 s 的边,容量为 INF. 通过上述步骤,现在图变成了无源汇网络. 引入超级源点 S,超级汇点 T. 连接一条 ...

  2. 最大表示法--环形字符串最大字典序(HDU 5442)

    http://acm.hdu.edu.cn/showproblem.php?pid=5442 问题概述:n个字符围成一个环,请从这个环中找出字典序最大的长度为n的字符串,输出它的起始点和方向(0顺1 ...

  3. HDU2019多校第二场 1009(HDU 6599) I Love Palindrome String(回文树(自动机)+manacher)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 解题思路: 回文自动机求每个本质不同的子串出现的次数,同时记录每个节点i代表的回文串第一次出现的 ...

  4. S-Nim (HDU 1536)组合博弈SG多组游戏

    S-Nim 题目链接 Problem Description Arthur and his sister Caroll have been playing a game called Nim for ...

  5. BestCoder25 1001.Harry and Magical Computer(hdu 5154) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5154 题目意思:有 n 门 processes(编号依次为1,2,...,n),然后给出 m 种关系: ...

  6. (HDU - 1847)Good Luck in CET-4 Everybody!(博弈)

    题目链接:Good Luck in CET-4 Everybody! - HDU 1847 - Virtual Judge (ppsucxtt.cn) 题目是中文的,我在这就不翻译题意了. 先说一种打 ...

  7. 美素数(HDU 4548)(打表,简化时间复杂度)

    相信大家都喜欢美的东西,让我们一起来看看美素数吧. 问题是这样的:一个十进制数,如果是素数,而且它的各位数字和也是素数,则称之为"美素数",如29,本身是素数,而且2+9 = 11 ...

  8. 单词数(HDU 2072)

    lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数.下面你的任务是帮助xiaoou333解决这个问题. Input 有多组数据,每组一行,每组就 ...

  9. Ant Trip(HDU 3018)---多笔画问题

    题目链接 题目描述 Ant Country consist of N towns.There are M roads connecting the towns. Ant Tony,together w ...

  10. 机器人的舞蹈(hdu 2232)

    机器人的舞蹈 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

最新文章

  1. python输入文字字符串、如何提取某个汉字_python提取字符串中的汉字数字字母
  2. fastJson反序列化异常,JSONException: expect ‘:‘ at 0, actual =
  3. esp32外部中断_玩转 ESP32 + Arduino (四) 电容按键 霍尔传感器 外部中断 延时 脉冲检测...
  4. CodeForces - 1422D Returning Home(最短路+思维建图)
  5. CF1131 G. Most Dangerous Shark (单调栈优化dp)
  6. 位操作符:与,或,异或 狼羊菜
  7. Debug和Release区别
  8. 彻底讲明白浅拷贝与深拷贝
  9. 翻译:where在Swift中的用法
  10. 易语言服务器ping,Ping网络IP地址易语言源码
  11. 局域网文件传输工具---LANDrop 使用记录
  12. 计算机专业学生实习目的,计算机专业应届毕业生实习目的
  13. 软件测试用例设计包括哪些类型?
  14. qqlive播放器下载视频
  15. 悟空问答 模板 html,悟空问答icon
  16. 机器人——人类的终极进化
  17. php百度坐标转腾讯坐标,PHP实现腾讯与百度坐标转换
  18. 国家企业信用信息公示系统每年申报登录提示账号不存在【山东】
  19. ps之一寸照片的制作详解(1)
  20. android打开cad卡顿,两万高配置电脑使用CAD非常卡顿怎么办

热门文章

  1. iphone中结束电话后返回自己的应用
  2. 汇编语言---GCC内联汇编
  3. 6个特征,判断你的领导值不值得追随
  4. S5PV210启动过程
  5. Spring Boot官宣:正式弃用 Java 8
  6. 在阿里工作5年了,斗胆谈谈我认为的高级开发到底应该是怎样的?
  7. 技术能变现,才是硬道理
  8. 阿里高级技术专家张建飞:应用架构分离业务逻辑和技术细节之道
  9. Spring Boot开发之流水无情(二)
  10. JEECG Online Coding 开发操作图解