康托展开(方便排列计数)

来源:互联网 发布:梦里花落知多少封面 编辑:程序博客网 时间:2024/05/17 23:27

 康托展开讲解



可以用下面的代码检测用康托扩展找到的序号是否正确:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int main() {int a[7] = {1,2, 3, 4, 5, 6 ,7};int cnt = 0; do{int ans = 0; for(int i = 0; i < 7; i ++) {ans = ans*10 + a[i];}++cnt;if(ans == 1237654) cout << ans << "   "<< cnt << endl;if(ans == 6534721) cout << ans << "   "<< cnt << endl;if(ans == 7534621) cout << ans << "   "<< cnt << endl;}while(next_permutation(a, a+7));return 0; }

大概的模板形式:

long long cantor() {long long ans = 0;  for(int i = 0; i < N; i++) {int cnt = 0; for(int j = i+1; j < N; j++) if(a[j] < a[i]) cnt++;ans += F[8-i]*cnt;}return ans;}


0 0