Cantor expansion and deCantor expansion
Posted rign
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cantor expansion and deCantor expansion相关的知识,希望对你有一定的参考价值。
Cantor expansion is a way to use the Full Permutation and the id in the Permutation.In this way to map the id and the permutation,we can create a easy hash.It can be said a the most easy hash.(ans=a_{0}cdot (n-1)!+a_{1}cdot(n-2)!+cdotcdotcdot+a_{n-1}cdot(n-(n-1))!+a_{n}cdot(n-n)!)the (a_{i}) is the num in the others.
deCantor expansion is a opposite progress.
#include<bits/stdc++.h>
using namespace std;
int f[]={1,1,2,6,24,120};
int a[5];
void contorExpanse(){//康拓展开
int ans=0;
for(int i=0;i<5;i++){
int tmp=0;
for(int j=i+1;j<5;j++){
if(a[j]<a[i]) tmp++;//计数 }
ans+=tmp*f[5-i-1];//当前计数*阶乘 }
cout<<ans<<endl;
}
void anticontorExpanse(int n){
//写的非常混乱
/*
整理一下思路就是设计两个动态数组作为可选和结果
从n->1开始,不断的辗转相除
计算当前小于该数的个数,从可选中选择,并删除
此时该数变成余数,重复步骤
*/
vector<int>k;
for(int i=1;i<=5;i++) k.push_back(i);
vector<int>contor;
int tmp=n;
for(int i=5;i>=1;i--){
int cc=tmp/(f[i-1]);//计算当前的计数 contor.push_back(k[cc]);
k.erase(k.begin()+cc);//删除
tmp=tmp%f[i-1];//变成余数 }
for(int i=0;i<contor.size();i++) cout<<contor[i]<<" ";
}
int main(){
for(int i=0;i<5;i++) cin>>a[i];
contorExpanse();
int n;
cin>>n;
anticontorExpanse(n);
return 0;
}
?
以上是关于Cantor expansion and deCantor expansion的主要内容,如果未能解决你的问题,请参考以下文章
cf1523C. Compression and Expansion
CF1523C. Compression and Expansion(堆栈模拟)