Codeforces554D: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, 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 length nthat
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.
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).
Print n space-separated integers, representing the permutation that is the answer for the question.
4 3
1 3 2 4
10 1
1 2 3 4 5 6 7 8 9 10
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,k。代表有包括1~n的一个数组。通过对这些数进行一些排列,对于当中的一个序列,第i个位置会指向第a[i]个位置,如此便会形成一些环,将这些环合并成一组。按大到小排序,然后对于形成的多组而言,依照每一组开头数字的大小从小大大排序,形成一个新的序列
而对于这些序列而言,当中有一些序列依照题意的分类排序方法得到的新序列是与本身相等的,如今要求这些序列中的第k个是多少。k不会超过这样的序列的总数
思路:
对于这样的类型的序列,那么必定是交换相邻的两个。并且已经交换过了的是不能再交换了,而当中数量又与斐波那契数有关系
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std; #define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 100005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7; LL n,k;
LL a[55]; int main()
{
LL i,j;
a[0] = a[1] = 1;
for(i = 2;i<=50;i++)
{
a[i] = a[i-1]+a[i-2];
}
scanf("%I64d%I64d",&n,&k);
LL c1 = 1,c2 = 2;
while(n>0)
{
if(k>a[n-1])
{
printf("%I64d %I64d ",c2,c1);
k-=a[n-1];
n-=2;
c2+=2;
c1+=2;
}
else
{
printf("%I64d ",c1);
n--;
c1++;
c2++;
}
}
printf("\n"); return 0;
}
Codeforces554D:Kyoya and Permutation的更多相关文章
- Codeforces Round #309 (Div. 1) B. Kyoya and Permutation 构造
B. Kyoya and Permutation Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Codeforces Round #309 (Div. 2) -D. Kyoya and Permutation
Kyoya and Permutation 这题想了好久才写出来,没看题解写出来的感觉真的好爽啊!!! 题目大意:题意我看了好久才懂,就是给你一个序列,比如[4, 1, 6, 2, 5, 3],第一个 ...
- codeforces 553B B. Kyoya and Permutation(找规律)
题目链接: B. Kyoya and Permutation time limit per test 2 seconds memory limit per test 256 megabytes inp ...
- Codeforces 553B Kyoya and Permutation
problem 题意 本题题意不太easy看懂. 给定一个序列,我们能够把这个序列变成一些循环置换的和.然而这样的置换的方法是不止一种的.我们定义一种standard cyclic represent ...
- 【Codeforces】【#295】【Div.1】
嘛,一直以来蒟蒻都没怎么打过CF……现在还是蓝名狗……今天跟着zyf打了一场virtual,果断一题滚粗
- Codeforces Round #309 (Div. 1)
A. Kyoya and Colored Balls 大意: 给定$k$种颜色的球, 第$i$种颜色有$c_i$个, 一个合法的排列方案满足最后一个第$i$种球的下一个球为第$i+1$种球, 求合法方 ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
随机推荐
- HDU 1535 Invitation Cards(SPFA,及其优化)
题意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费. 有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1. ...
- KMP算法(具体求串的next[n])
怎么求串的模式值next[n] )next[0]= -1 意义:不论什么串的第一个字符的模式值规定为-1. )next[j]= -1 意义:模式串T中下标为j的字符,假设与首字符 同样,且 ...
- [置顶] 深圳华为BSS公共部件 (BI 商业智能 Java Javascript)
深圳华为BSS公共部件 部门招聘 招聘面试地点:大连,西安 工作地点:深圳 时间:2013年9月7日 联系方式:dawuliang@gmail.com 18675538182 有兴趣的同学,可以直接电 ...
- tar.gz文件命名和压缩解压方法
tar.gz文件命名 tar这是文件打成一个包,无压缩; gz同gzip标记的包.tar文件压缩; 所以它成为一个.tar.gz档 压缩 # tar cvfz backup.tar.gz /xxx/ ...
- [C++]函数指针与指针函数
函数指针与指针函数,之前总是分不清,今天就好好把它两认认清楚. 先从概念说起,简单是理解. 函数指针:是指针.一个指向某一个函数入口地址的指针. 指针函数:是函数.一个返回值是指针的函数. 记忆: 从 ...
- Linux ssh密钥自动登录(转)
在开发中,经常需要从一台主机ssh登陆到另一台主机去,每次都需要输一次login/Password,很繁琐.使用密钥登陆就可以不用输入用户名和密码了 实现从主机A免密码登陆到主机B,需要以下几个步骤: ...
- Spark简述及基本架构
Spark简述 Spark发源于美国加州大学伯克利分校AMPLab的集群计算平台.它立足 于内存计算.从多迭代批量处理出发,兼收并蓄数据仓库.流处理和图计算等多种计算范式. 特点: 1.轻 Spark ...
- HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP
分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...
- HDU 4814 Golden Radio Base 模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4814 题目大意: 把一个正整数表示为φ进制, φ = (1+√5)/2 . 且已知: 1. φ + 1 ...
- 在不同版本号hdfs集群之间转移数据
在不同版本号hdfs集群之间转移数据 最简单的办法就是把src集群的数据导到本地,然后起还有一个进程将本地数据传到des集群上去. 只是这有几个问题: 效率减少 占用本地磁盘空间 不能应付实时 ...