DeCantor Expansion (逆康托展开)
Background\text{Background}Background
The \text{The }The Listen&Say Test will be hold on May 11, so I decided to fill my blog \text{Test will be hold on May 11, so I decided to fill my blog }Test will be hold on May 11, so I decided to fill my blog
with English words until that day.\text{with English words until that day.}with English words until that day.
Problem\text{Problem}Problem
There goes a problem.\text{There goes a problem.}There goes a problem.
You’ve got 2 intergers N,k. Please calculate the kth permutation of ∀k∈[i,n].\text{You've got 2 intergers }N,k\text{. Please calculate the }k\text{th permutation of }\forall k\in[i,n].You’ve got 2 intergers N,k. Please calculate the kth permutation of ∀k∈[i,n].
Solution\text{Solution}Solution
It’s easy to know that we can got it by Depth-first Search, \text{It's easy to know that we can got it by Depth-first Search, }It’s easy to know that we can got it by Depth-first Search, but its Time complexity is O(n!).\text{but its Time complexity is }O(n!).but its Time complexity is O(n!).
DeCantor Expansion is a algorithm which can solve problems like these calculating the kth permutation\text{is a algorithm which can solve problems like these calculating the }k\text{th permutation}is a algorithm which can solve problems like these calculating the kth permutationin O(nlogn) with heap optimization.\text{in }O(n\log n)\text{ with heap optimization.}in O(nlogn) with heap optimization.
Let’s explain how it works in a simple example. Set N=5,k=61, the answer is a[].\text{Let's explain how it works in a simple example. Set }N=5,k=61,\text{ the answer is }a[].Let’s explain how it works in a simple example. Set N=5,k=61, the answer is a[].
1.Let 61 / 4! = 2 ... 13, it shows that there’re 2 numbers behind a[1] are smaller than a[1].\text{1.\quad Let 61 / 4! = 2 ... 13, it shows that there're 2 numbers behind }a[1]\text{ are smaller than a[1].}1.Let 61 / 4! = 2 ... 13, it shows that there’re 2 numbers behind a[1] are smaller than a[1].
Therefore, a[1]=3;\text{Therefore, }a[1]=3;Therefore, a[1]=3;
2.Let 13 / 3! = 2 ... 1, it shows that there’re 2 numbers behind a[2] are smaller than a[2].\text{2.\quad Let 13 / 3! = 2 ... 1, it shows that there're 2 numbers behind }a[2]\text{ are smaller than a[2].}2.Let 13 / 3! = 2 ... 1, it shows that there’re 2 numbers behind a[2] are smaller than a[2].
Therefore, a[2]=4;\text{Therefore, }a[2]=4;Therefore, a[2]=4;
3.Let 1 / 2! = 0 ... 1, it shows that there’re 0 number behind a[3] are smaller than a[3].\text{3.\quad Let 1 / 2! = 0 ... 1, it shows that there're 0 number behind }a[3]\text{ are smaller than a[3].}3.Let 1 / 2! = 0 ... 1, it shows that there’re 0 number behind a[3] are smaller than a[3].
Therefore, a[3]=1;\text{Therefore, }a[3]=1;Therefore, a[3]=1;
4.Let 1 / 1! = 1 ... 0, it shows that there’re 1 number behind a[4] are smaller than a[4].\text{4.\quad Let 1 / 1! = 1 ... 0, it shows that there're 1 number behind }a[4]\text{ are smaller than a[4].}4.Let 1 / 1! = 1 ... 0, it shows that there’re 1 number behind a[4] are smaller than a[4].
Therefore, a[4]=5;\text{Therefore, }a[4]=5;Therefore, a[4]=5;
Therefore, a[5]=2,a[]={3,4,1,5,2}.\text{Therefore, }a[5]=2, a[]=\{3,4,1,5,2\}.Therefore, a[5]=2,a[]={3,4,1,5,2}.
Summary\text{Summary}Summary
∀i∈[1,n−1], let k / (n−1)!, the answer you’ve got is the number of interger j∈[i+1,n] which has a[j]<a[i]. And let k equals to the remainder.\forall i\in[1,n-1],\text{ let }k\ /\ (n-1)!\text{, the answer you've got is the number of interger }\newline j\in[i+1,n]\text{ which has }a[j]<a[i].\text{ And let }k\text{ equals to the remainder.}∀i∈[1,n−1], let k / (n−1)!, the answer you’ve got is the number of interger j∈[i+1,n] which has a[j]<a[i]. And let k equals to the remainder.
The End\text{The End}The End
Reference material\text{Reference material}Reference material
DeCantor Expansion (逆康托展开)的更多相关文章
- LightOJ1060 nth Permutation(不重复全排列+逆康托展开)
一年多前遇到差不多的题目http://acm.fafu.edu.cn/problem.php?id=1427. 一开始我还用搜索..后来那时意外找到一个不重复全排列的计算公式:M!/(N1!*N2!* ...
- nyoj 139——我排第几个|| nyoj 143——第几是谁? 康托展开与逆康托展开
讲解康托展开与逆康托展开.http://wenku.baidu.com/view/55ebccee4afe04a1b071deaf.html #include<bits/stdc++.h> ...
- 题解报告:NYOJ 题目143 第几是谁?(逆康托展开)
描述 现在有"abcdefghijkl”12个字符,将其按字典序排列,如果给出任意一种排列,我们能说出这个排列在所有的排列中是第几小的.但是现在我们给出它是第几小,需要你求出它所代表的序列. ...
- HDU1027 Ignatius and the Princess II( 逆康托展开 )
链接:传送门 题意:给出一个 n ,求 1 - n 全排列的第 m 个排列情况 思路:经典逆康托展开,需要注意的时要在原来逆康托展开的模板上改动一些地方. 分析:已知 1 <= M <= ...
- 康托展开&逆康托展开学习笔记
啊...好久没写了...可能是最后一篇学习笔记了吧 题目大意:给定序列求其在全排列中的排名&&给定排名求排列. 这就是康托展开&&逆康托展开要干的事了.下面依次介绍 一 ...
- Codeforces-121C(逆康托展开)
题目大意: 给你两个数n,k求n的全排列的第k小,有多少满足如下条件的数: 首先定义一个幸运数字:只由4和7构成 对于排列p[i]满足i和p[i]都是幸运数字 思路: 对于n,k<=1e9 一眼 ...
- hdoj 1027 Ignatius and the Princess II 【逆康托展开】
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- 康托展开与逆康托展开模板(O(n^2)/O(nlogn))
O(n2)方法: namespace Cantor { ; int fac[N]; void init() { fac[]=; ; i<N; ++i)fac[i]=fac[i-]*i; } in ...
- 【HDU - 1043】Eight(反向bfs+康托展开)
Eight Descriptions: 简单介绍一下八数码问题:在一个3×3的九宫格上,填有1~8八个数字,空余一个位置,例如下图: 1 2 3 4 5 6 7 8 在上图中,由于右下角位置是空的 ...
随机推荐
- 前端黑魔法:webworker动态化,无需JS文件创建worker
前言 前几天,我和一位知乎网友讨论这个问题的时候,觉得这非常有意思,所以写了这篇文章作为记录 本文的思路和项目代码来源于知友 @simon3000,我加以修饰以更符合理解的需求. 本文所用代码已经 ...
- error LNK1104: 无法打开文件“opencv_world331.lib” LINK : fatal error LNK1104: 无法打开文件“opencv_world331.lib”,程序报这个错误时应该怎么解决?
这几天被这个错误搞得很头大,看了很多大神的解决办法,涉及到opencv的版本的windows的操作系统等各种知识,但是我尝试了很多办法最终都没有解决.今天晚上突然想到之前遇到这种无法打开lib文件时, ...
- java必学技能
一:系统架构师是一个最终确认和评估系统需求,给出开发规范,搭建系统实现的核心构架,并澄清技术细节.扫清主要难点的技术人员.主要着眼于系统的“技术实现”.因此他/她应该是特定的开发平台.语言.工具的大师 ...
- celery详解
目录 Celery详解 1.背景 2.形象比喻 3.celery具体介绍 3.1 Broker 3.2 Backend 4.使用 4.1 celery架构 4.2 安装redis+celery 4.3 ...
- TestNG(一) TestNG实战在idea中创建module
1.在ider里创建一个Module 2.直接点击下一步 3.输入Groupld h和Artifactid名称,点击下一步 4.点击Finish 创建完成
- IO流 - 字节输入输出流,文件的复制
IO流 I:input - 输入(读取),eg:把硬盘的内容读取到内存 O: output - 输出(写入) eg:把内存中的东西写入硬盘保存 流:数字(字符/字节) 一般1个字符=2Byte,1By ...
- 不fq安装 golang tools
go get -u -v github.com/golang/tools/go/buildutil ln -s $GOPATH/src/github.com/golang/tools $GOPATH/ ...
- Python + opencv 实现图片文字的分割
实现步骤: 1.通过水平投影对图形进行水平分割,获取每一行的图像: 2.通过垂直投影对分割的每一行图像进行垂直分割,最终确定每一个字符的坐标位置,分割出每一个字符: 先简单介绍一下投影法:分别在水平和 ...
- python-字符编码、字符串格式化、进制转化、数据类型、列表、元组、字典总结
目录: 一.字符编码 二.字符串格式化 三.进制转换 四.数据类型及其操作 五.字符串转换 六.列表 七.元组 八.字典 一.字符编码: 计算机由美国人发明,最早的字符编码为ASCII,只规定了英文字 ...
- maven 项目报错org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)解决
idea在使用maven构建的项目中使用mybatis时报错org.apache.ibatis.binding.BindingException: Invalid bound statement (n ...