codeforces 553B B. Kyoya and Permutation(找规律)

题目链接:

B. Kyoya and Permutation

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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, permutationp = [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 lengthn 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 nk (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.

Examples
input
4 3
output
1 3 2 4
input
10 1
output
1 2 3 4 5 6 7 8 9 10

题意:

1到n的一个排列,问经过上面的那两步操作后得到的序列不变,把这些序列按字典序从小到大排列,问第k个是什么;

思路:

可以发现所有的排列要满足条件都是相邻的进行交换,长度为n的满足要求的个数为dp[n]=dp[n-1]+dp[n-2];
现在对于第i位要看是否需要交换,如果不交换,说明后面长为n-i的序列的个数dp[n-i]>=k,否则这一位就要交换了,交换了那么k的大小也要变化了;就这样确定每一位上的数;

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>
 
using namespace std;
 
#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
 
typedef  long long LL;
 
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
    for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + '0');
    putchar('
');
}
 
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=3e6+10;
const int maxn=1e3+20;
const double eps=1e-12;

LL a[52],k;
int n,ans[52];
int main()
{
    read(n);read(k);
    a[0]=a[1]=1;a[2]=2;
    for(int i=3;i<=n;i++)a[i]=a[i-1]+a[i-2];
    int pos=1;
    while(pos<=n)
    {
        if(k<=a[n-pos])
        {
            ans[pos]=pos;
            pos++;
        }
        else 
        {
            ans[pos]=pos+1;
            ans[pos+1]=pos;
            k-=a[n-pos];
            pos+=2;
        }
    }
    for(int i=1;i<=n;i++)printf("%d ",ans[i]);
    return 0;
}

  

原文地址:https://www.cnblogs.com/zhangchengc919/p/5789360.html