*技巧(10)斐波那契数列(a[n]=a[n-1]+a[n-2])
Kyoya and Permutation
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.
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].
先考虑两个数n,m(n<m)构成环,如果不相邻,设n,m中间为k。
k映射n,m中间的数 :m,k,n -> (k...),(m,n);
k映射大于m的数 :m,k,n -> (m,n),(k...);
k映射小于n的数 :m,k,n -> (k...),(m,n);
So,只有m,n相邻时才能保证 NO change ! m,n -> (m,n);
再考虑多个数,以3个数为例。设相邻的三个数 n <k< m; 有:(n,k,m) : (k,m,n) -> (m,k,n) -> (k),(m,n);
几次变换之后,ta又变成了不相邻的两个数n,m交换,so,多个数构成环不成立。
SO,在全部数各自映射的基础上,进行相邻数的互换,并且每个数只能互换一次!
a[n] =a[n-1] ((1,2)不换)+ a[n-2]((1,2)换);
1
1 2
2 1
1 2 3 1 2
1 3 2 2 1
2 1 3 1
1 2 3 4 1 2 3
1 2 4 3 1 3 2
1 3 2 4 2 1 3
2 1 3 4 1 2
2 1 4 3 2 1
1 2 3 4 5 1 2 3 4
1 2 3 5 4 1 2 4 3
1 2 4 3 5 1 3 2 4
1 3 2 4 5 2 1 3 4
1 3 2 5 4 2 1 4 3
2 1 3 4 5 1 2 3
2 1 3 5 4 1 3 2
2 1 4 3 5 2 1 3
#include<cstdio> #define ll __int64 const int maxn = 55; using namespace std; ll a[maxn]; int main(){ int n; ll m; scanf("%d%I64d",&n,&m); a[0] = 1;a[1] = 1; for(int i = 2; i <= n; i++) a[i] = a[i-1] + a[i-2]; bool flag = 0; ll c1 = 1,c2 = 2; while(n){ if(m > a[n-1]){ if(flag)printf(" "); flag = 1; printf("%I64d %I64d",c2,c1); c1 += 2; c2 += 2; m -= a[n-1]; n -= 2; } else { if(flag)printf(" "); flag = 1; printf("%I64d",c1); c1++; c2++; n--; } } printf("\n"); return 0; }

浙公网安备 33010602011771号