2019 杭电多校 5 1005

题目链接:HDU 6628

比赛链接:2019 Multi-University Training Contest 5

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 \(p_1, p_2,...,p_n\) as \(p_2−p_1,p_3−p_2,...,p_n−p_{n−1}\). In other words, the length of the difference sequence is \(n−1\) and the \(i\)-th term is \(p_{i+1}−p_i\)

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\le K\le min(10^4, 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

Solution

题意:

定义排列 \(p_1, p_2, ... , p_n\) 的 "difference sequence" 为 \(p_2-p_1, p_3-p_2,...,p_n-p_{n-1}\)。现在给定 \(N\) 和 \(K\),求长度为 \(N\) 的所有排列中 "difference sequence" 的字典序第 \(K\) 小的排列。

题解:

暴力 STL 全排列

题目给定 \(K\) 的范围不超过 \(10^4\),而 \(8! = 40320 > K\),因此可以预处理 \(N <= 8\) 的情况,当 \(N > 8\) 时暴力求 \(a[1] = n\) 的全排列。

Code

#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10; struct P {
int num[10];
string str;
} ans[10][maxn]; int cmp(P p1, P p2) {
return p1.str < p2.str;
} int main() {
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
for(int i = 2; i <= 8; ++i) {
int cnt = 1;
do {
// for(int j = 0; j < i; ++j) {
// cout << a[j] << " ";
// }
// cout << endl;
for(int j = 1; j <= i; ++j) {
ans[i][cnt].num[j] = a[j];
if(j < i) ans[i][cnt].str += a[j + 1] - a[j] + 'A';
}
++cnt;
} while(next_permutation(a + 1, a + 1 + i));
sort(ans[i] + 1, ans[i] + cnt, cmp);
// for(int j = 1; j < cnt; ++j) { for(int k = 1; k <= i; ++k) cout << ans[i][j].num[k] << ""; cout << endl;}
}
int T;
cin >> T;
while(T--) {
int n, k;
scanf("%d%d", &n, &k);
if(n <= 8) {
for(int j = 1; j <= n; ++j) {
printf("%d", ans[n][k].num[j]);
printf("%s", j == n? "\n": " ");
}
} else {
int a[30];
a[1] = n;
for(int i = 2; i <= n; ++i) {
a[i] = i - 1;
}
for(int i = 0; i < k - 1; ++i) {
next_permutation(a + 1, a + 1 + n);
}
for(int i = 1; i <= n; ++i) {
printf("%d", a[i]);
printf("%s", i == n? "\n": " ");
}
}
}
return 0;
}

HDU 6628 permutation 1 (暴力)的更多相关文章

  1. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  2. hdu 5461 Largest Point 暴力

    Largest Point Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  3. hdu 5762 Teacher Bo 暴力

    Teacher Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5762 Description Teacher BoBo is a geogra ...

  4. hdu 5753 Permutation Bo 水题

    Permutation Bo 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  5. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  6. HDU 1333 基础数论 暴力

    定义一种数位simth数,该数的各位之和等于其所有质因子所有位数字之和,现给出n求大于n的最小该种数,n最大不超过8位,那么直接暴力就可以了. /** @Date : 2017-09-08 14:12 ...

  7. HDU 4618 Palindrome Sub-Array 暴力

    Palindrome Sub-Array 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4618 Description A palindrome s ...

  8. HDU 2089 不要62 | 暴力(其实是个DP)

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2089 题解: 暴力水过 #include<cstdio> #include<algor ...

  9. HDU 6115 Factory LCA,暴力

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6115 题意:中文题面 分析:直接维护LCA,然后暴力枚举集合维护答案即可. #include < ...

随机推荐

  1. PHP的Session机制解析 1

    一.php的默认session机制 php默认用磁盘文件来实现session在php.ini中session.save_handler = files定义session机制session.save_p ...

  2. ceph-报错日志

    由于时钟不一致问题,导致ceph存储有问题 clock skew时钟偏移overalladj. 全部的:全体的:一切在内的stampedadj. 铭刻的:盖上邮戳的:顿足的 beaconvt. 照亮, ...

  3. 85、使用TFLearn实现iris数据集的分类

    ''' Created on 2017年5月21日 @author: weizhen ''' #Tensorflow的另外一个高层封装TFLearn(集成在tf.contrib.learn里)对训练T ...

  4. 避开PCB假八层结构的温柔陷阱---浅谈六层板的叠层

    https://blog.csdn.net/qijitao/article/details/51505611 作者:王辉东   一博科技高速先生团队队员 在<PCB的筋骨皮>一文中,我们提 ...

  5. java.lang -> Object

    java.lang -> Object 是什么 Object 类是类层次结构的根,是 Java 中唯一一个没有父类的类,Java 中所有对象包括数组都继承了 Object 类中的方法. 重要方法 ...

  6. 基于MFC的Media Player播放器的制作介绍

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 因为这次多媒体课程设计做一个基于MFC的播放器,因为本人实力太菜,需要播放音乐或视频文件时候,自己写不出解码 函数,所以准备使用第三方多媒 ...

  7. Eclipse国内下载升级方法

    Eclipse国内下载升级方法 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} ...

  8. jumpserver注意事项以及报错处理

    需要注意下面亮点 在使用jumpserver过程中,有一步是系统用户推送,要推送成功,client(后端服务器)要满足以下条件: 后端服务器需要有python.sudo环境才能使用推送用户,批量命令等 ...

  9. SpringMVC学习(4):数据绑定1 @RequestParam

    在系列(3)中我们介绍了请求是如何映射到一个action上的,下一步当然是如何获取到请求中的数据,这就引出了本篇所要讲的内容-数据绑定. 首先看一下都有哪些绑定数据的注解: 1.@RequestPar ...

  10. ASE——第一次结对作业

    ASE--第一次结对作业 问题定义 很早就听说了MSRA的黄金点游戏,让大家写Bot来参加比赛看谁的AI比较聪明可以操盘割韭菜.深感ASE课程老师设计的任务太用心了,各种接口都准备好了,大家只用专注于 ...