#include <iostream>
#include <cstdio>
using namespace std;
const int M = 16;
struct node{
node(int _v = 0, int _n = 0): value(_v), num(_n){
for(int i = 0; i < M; i++) next[i] = NULL;
}
int value, num;
node* next[M];
};
const int N = 290;
int a[N];
int *p;
int tree[9] = {0,1,17,273,4369,69905,1118481,17895697,286331153};
int calChildNum(int total, int remain, int index){
if(total - tree[index+1] >= (remain - 1) * tree[index]) return tree[index+1];
else return total - (remain - 1) * tree[index];
}
node* creat(int num){
node* buf = new node(*p++, num);
int index = 0;
for(int i = 8; i >= 0; i--) {
if(num > tree[i]) {
index = i - 1;
break;
}
}
num--;
for(int i = M; i > 0; i--){
if(num <= 0) break;
int k = calChildNum(num, i, index);
num -= k;
if(k > 0) buf->next[M-i] = creat(k);
}
return buf;
}
void print(node* root){
cout << root->value << endl;
for(int i = 0; i < M; i++)if(root->next[i]) print(root->next[i]);
}
int get(node* root, int index){
if(index == 1) return root->value;
else{
index--;
for(int i = 0; i < M; i++){
if(index <= root->next[i]->num) return get(root->next[i], index);
index -= root->next[i]->num;
}
}
}
int main(){
for(int i = 0; i < N-1; i++) a[i] = i + 1;
p = a;
node* root = creat(N-1);
for(int i = 0; i < N-1; i++) cout << get(root, i + 1) << endl;
return 0;
}
不可变数据结构中的数组是用16叉树或者32叉树实现的,自己用c++写了写,发现不适合用命令式语言搞这些东西,而且我的效率很差
真不知道函数式语言是如何实现的