# 康托展开（Cantor Expansion）例题 + 略解

## 康托展开的定义&公式

——维基百科

\displaystyle X = A_n * (n-1)! + A_{n-1} * (n-2)! + \dots + A_i * (i-1)! + \dots + A_2 * 1! + A_1 * 0!

$\displaystyle 0 * (5-1)! + 1 * (4-1)! + 0 * (3-1)! + 1 * (2-1)! + 0 * (1-1)! = 7$

1 2 3 4 5
1 2 3 5 4
1 2 4 3 5
1 2 4 5 3
1 2 5 3 4
1 2 5 4 3
1 3 2 4 5
1 3 2 5 4

## 康托展开的逆

1. 将 $k$ 减去 1，此时 $k$ 就代表了比我们要求的排列小的排列有几个（参考前面，我们最后把求出的答案+1了）；

2. $k$ 除以 $(N-1)!$，得到 $A_n$

3. $k$ 除以 $(N-2)!$，得到 $A_{n-1}$

4. ...

## 例题

### 题目描述

The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of their crazy games with Farmer John. The cows will arrange themselves in a line and ask Farmer John what their line number is. In return, Farmer John can give them a line number and the cows must rearrange themselves into that line.

A line number is assigned by numbering all the permutations of the line in lexicographic order.

Consider this example:

Farmer John has 5 cows and gives them the line number of 3.

The permutations of the line in ascending lexicographic order: 1st: 1 2 3 4 5

2nd: 1 2 3 5 4

3rd: 1 2 4 3 5

Therefore, the cows will line themselves in the cow line 1 2 4 3 5.

The cows, in return, line themselves in the configuration '1 2 5 3 4' and ask Farmer John what their line number is.

Continuing with the list:

4th : 1 2 4 5 3

5th : 1 2 5 3 4

Farmer John can see the answer here is 5

Farmer John and the cows would like your help to play their game. They have K (1 <= K <= 10,000) queries that they need help with. Query i has two parts: C_i will be the command, which is either 'P' or 'Q'.

If C_i is 'P', then the second part of the query will be one integer A_i (1 <= A_i <= N!), which is a line number. This is Farmer John challenging the cows to line up in the correct cow line.

If C_i is 'Q', then the second part of the query will be N distinct integers B_ij (1 <= B_ij <= N). This will denote a cow line. These are the cows challenging Farmer John to find their line number.

### 输入输出格式

• Line 1: Two space-separated integers: N and K

• Lines 2..2K+1: Line 2i and 2*i+1 will contain a single query.

Line 2*i will contain just one character: 'Q' if the cows are lining up and asking Farmer John for their line number or 'P' if Farmer John gives the cows a line number.

If the line 2*i is 'Q', then line 2*i+1 will contain N space-separated integers B_ij which represent the cow line. If the line 2*i is 'P', then line 2*i+1 will contain a single integer A_i which is the line number to solve for.

• Lines 1..K: Line i will contain the answer to query i.

If line 2*i of the input was 'Q', then this line will contain a single integer, which is the line number of the cow line in line 2*i+1.

If line 2*i of the input was 'P', then this line will contain N space separated integers giving the cow line of the number in line 2*i+1.

### 输入输出样例

5 2
P
3
Q
1 2 5 3 4 

1 2 4 3 5
5 

## 参考代码

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=25;
int n,m,a[maxn],b[maxn];
bool vis[maxn];
int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
long long ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*(long long)10+(long long)(ch-'0'),ch=getchar();
return (long long)ret*f;
}
char ch=getchar();
while (ch!='P'&&ch!='Q') ch=getchar();
return ch;
}
inline bool check(int L,int R){
for (int i=L;i<=R;i++) if (vis[i]) return 1;
return false;
}
int main(){
for (int k=1;k<=m;k++){
if (ch=='Q'){
long long fac=1;
fac=(long long)fac/(long long)n;
long long x=0;int cnt=n-1;
for (int i=1;i<=n;i++){
int now=0; //now表示这个排列里（从左到右）第i个数字之后有多少比这个数字小的数字
for (int j=i+1;j<=n;j++) if (a[i]>a[j]) now++;
x=(long long)x+(long long)now*(long long)fac;
if (cnt) fac=(long long)fac/(long long)cnt--;
}
printf("%lld\n",++x);
} else if (ch=='P'){
for (int i=2;i<n;i++) fac=fac*(long long)i;
for (int i=n;i>=1;i--){
a[n-i+1]=(long long)x/fac;
x%=fac;
if (i-1) fac=fac/(long long)(i-1); else fac=1;
}
memset(vis,0,sizeof(vis));
for (int i=1;i<=n;i++){ //B[i]即i位上的数字
int cnt=0;b[i]=-1;
for (int j=0;j<=n;j++){
if (vis[j]) cnt++;
if ((!vis[cnt+a[i]+1])&&(!check(j+1,cnt+a[i]))){
vis[cnt+a[i]+1]=1;b[i]=cnt+a[i]+1;break;
}
}
}
for (int i=1;i<n;i++) printf("%d ",b[i]);printf("%d\n",b[n]);
}
}
return 0;
}

# Related posts

1. Dalao更新Blog了好稳啊Orz