Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 找规律
2015-07-16 18:23
471 查看
B. Kyoya and Permutation
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s define the permutation of length n as an array p = [p1, p2, …, pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don’t change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya’s list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya’s list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Sample test(s)
input
4 3
output
1 3 2 4
input
10 1
output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
题意不太好理解,题目说,定义cycle为一个1-n的排列,要求从某位映射几次后又回到本身了,比如题中的 1 - 1 2 - 3 - 2 4 - 4 ,其实每个排列都是有这样的规律,如随更说个 4 1 2 3,就是一个cycle 1 - 4 - 3 - 2 - 1,题目又定义合法的cycle为每个循环节内部按从大到小排序,循环节间用从小到大排序之后得到的序列与原来一致才是合法的序列,如 (1)(32)(4)排序后还是 (1)(32)(4),所以是合法的,但4 1 2 3 排序后为4 3 2 1,与原来不一致,所以是不合法的!
好吧,题目有点绕,我先是理解错了题意。把1 2 3 4列出来,我们发现有这些是合法的
1 2 3 4
1 2 4 3
1 3 2 4
2 1 3 4
2 1 4 3
很明显,是不是,只有相邻的两位可以交换,某一位要么不交换,要么只能交换一次,用dp的思想,dp
= dp[n-1] +dp[n-2],就是每n位不交换,或都与下一位交换。这不就是斐波那契数列嘛,发现这个规律后,就可以如这一位比dp
大,则这一位就要交换,否则不用交换。代码就很简单了。
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s define the permutation of length n as an array p = [p1, p2, …, pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p = [4, 1, 6, 2, 5, 3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4, 1, 6, 2, 5, 3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4, 1, 6, 2, 5, 3] will become [4, 2, 1, 5, 6, 3].
Kyoya notices that some permutations don’t change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya’s list.
Input
The first line will contain two integers n, k (1 ≤ n ≤ 50, 1 ≤ k ≤ min{1018, l} where l is the length of the Kyoya’s list).
Output
Print n space-separated integers, representing the permutation that is the answer for the question.
Sample test(s)
input
4 3
output
1 3 2 4
input
10 1
output
1 2 3 4 5 6 7 8 9 10
Note
The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1, 2, 3, 4], while the second permutation would be [1, 2, 4, 3].
题意不太好理解,题目说,定义cycle为一个1-n的排列,要求从某位映射几次后又回到本身了,比如题中的 1 - 1 2 - 3 - 2 4 - 4 ,其实每个排列都是有这样的规律,如随更说个 4 1 2 3,就是一个cycle 1 - 4 - 3 - 2 - 1,题目又定义合法的cycle为每个循环节内部按从大到小排序,循环节间用从小到大排序之后得到的序列与原来一致才是合法的序列,如 (1)(32)(4)排序后还是 (1)(32)(4),所以是合法的,但4 1 2 3 排序后为4 3 2 1,与原来不一致,所以是不合法的!
好吧,题目有点绕,我先是理解错了题意。把1 2 3 4列出来,我们发现有这些是合法的
1 2 3 4
1 2 4 3
1 3 2 4
2 1 3 4
2 1 4 3
很明显,是不是,只有相邻的两位可以交换,某一位要么不交换,要么只能交换一次,用dp的思想,dp
= dp[n-1] +dp[n-2],就是每n位不交换,或都与下一位交换。这不就是斐波那契数列嘛,发现这个规律后,就可以如这一位比dp
大,则这一位就要交换,否则不用交换。代码就很简单了。
#define N 60 #define M 100005 #define maxn 205 #define MOD 1000000000000000007 ll dp ,k; int n; int main() { dp[0] = dp[1] = 1; for(int i = 2;i<N;i++) dp[i] = dp[i-1] + dp[i-2]; while(S(n) != EOF){ cin>>k; for(int i= n-1;i>=0;i--){ if(k > dp[i]){ k -= dp[i]; printf("%d %d ",n-i + 1,n-i); i--; } else printf("%d ",n-i); } } return 0; }
相关文章推荐
- android RecyclerView 资源汇总
- iOS 设置程序的图片 以及启动画面
- Linux 下 Sublime Text 中文输入问题的解决方案
- 【大型网站技术实践】初级篇:借助LVS+Keepalived实现负载均衡
- TOJ 3859 Letter Order
- LInux
- start
- 在网页中浏览PDF文档
- PWLRU:一种面向闪存数据库的缓冲区存取算法论文总结
- EIGRP协议的配置
- Charles — mac下网络封包分析工具
- ubuntu下QT与opencv的配置安装
- 输入a,n 计算a+aa+.....+aaaaa 加到n个a
- 黑马程序员-C语言基础知识-运算符与表达式
- BZOJ 1072: [SCOI2007]排列perm( )
- Red and Black
- 如何使用mysql 命令行 查看mysql表大小、数据大小、索引大小
- c++.primer.plus第五版第八章编程练习答案
- 框架基础学习之--web.xml文件详解
- Codeforces Round #312 (Div. 2) 题解