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. 查看静态库(.lib)和动态库(.dll)的导出函数的信息 error LNK2001: 无法解析的外部符号 _Delete

    转自VC错误:http://www.vcerror.com/?p=1381 在window下查看动态库的导出函数可以用vs自带的Dependenc工具: 查看静态库的信息要用命令行来实现: 首先运行V ...

  2. python字符串比较大小

    zfill函数 xs = ['] print (sorted(xs))

  3. nginx -stream(tcp连接)反向代理配置 实现代理mysql以及文件上传

    原文链接:https://blog.csdn.net/Activity_Time/article/details/95767390 1. stream模块安装 nginx默认安装的时候无法加载流str ...

  4. Springboot的Mybatis逆向工程

    1.pom.xml添加mybatis和逆向插件依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> ...

  5. 使用python+ffmpeg批量转换格式

    需求:  给定一个文件夹路径,遍历该文件夹内的所有文件以及子文件夹内的文件,当所有后缀名为wav格式的文件转换为ogg格式的文件. import os # 获取目录下的所有文件列表 import fn ...

  6. 你不知道的USB

    USB的接口类型.定义和原理 目前USB接口类型已经更新到了USB3.1和USB Type-C类型,下面就对USB的类型进行介绍整理 一.UCB的通信协议类型 1.1 USB定义及类型 USB(Uni ...

  7. visual studio 自定义警告标签

    写代码中经常会遇见某段代码写的不大合适或者是有隐患,但是一时半会有没时间不去完善,总体上不影响程序,也就放下了 结果时间一久就给忘了 vs提供了自定义警告的功能,这样就能有个提醒啦,方便以后改进 us ...

  8. Java对象什么时候可以被垃圾回收?JVM的永久代中会发生垃圾回收么?

    当对象对当前使用这个对象的应用程序变得不可触及的时候,这个对象就可以被回收了.垃圾回收不会发生在永久代,如果永久代满了或者是超过了临界值,会触发完全垃圾回收(Full GC).如果你仔细查看垃圾收集器 ...

  9. koa-artTemplate 的使用

    1.父页面 <html> <head> <meta charset="UTF-8"> <title>我的音乐</title&g ...

  10. MySQL数据库操作:“增删改查”,忘记密码重置等。

    [注] 数据库的“增删查改”,参考原作者Wid:http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#d11.感谢大佬们的技术分享 ...