X=a[n]*(n-1)!+a[n-1]*(n-2)!+...+a[i]*(i-1)!+...+a[1]*0!
其中a[i]表示第0到第i-1位有多少比第i位小。
const int dis[12] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800};
int Cantor(int *arg, int len)
{
int resl = 1;
for(int i = 0; i < len; i++){
int counts = 0;
for(int k = i + 1; k < len; k++){
if(arg[i] > arg[k]){
counts++;
}
}
resl = resl + dis[len-i-1] * counts;
}
return resl;
}
void InvCantor(int *arg, int num, int len)
{
num = num - 1;
bool vis[12] = {false};
for(int i = 0; i < len; i++){
int mid = 0;
int cnt = num / dis[len-i-1];
while(++mid){
if(!vis[mid]){
if(cnt == 0)
break;
cnt--;
}
}
arg[i] = mid;
vis[mid] = true;
num = num % dis[len-i-1];
}
}