码迷,mamicode.com
首页 > 其他好文 > 详细

Cantor expansion and deCantor expansion

时间:2018-12-12 00:06:45      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:html   attr   结果   var   keyword   展开   pos   选中   exce   

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)!+\cdot\cdot\cdot+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

标签:html   attr   结果   var   keyword   展开   pos   选中   exce   

原文地址:https://www.cnblogs.com/rign/p/10105591.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!