#include<stdio.h>
#include<math.h>
#include <malloc.h>
#define Typedef unsigned
struct Nodes{//结构体 定义节点数据结构
Typedef x;
Typedef y;
};
void IniteTree(unsigned N,Nodes *Tree)//对二叉树顺序存储数组初始化
{
Typedef i;
Typedef j;
for (i = 1;i<=N;i++)
{
// j = 1+ (int)(log10(i)/log10(2));
Tree[i].x = i;//由上至下从左至右顺序存储的序号
//
Tree[i].y = j;//所在层
}
}
template <class T>
T *FindLCA(int m,int j,T *BinaryTree)//计算最近共同根
{
int temp = j;
//j的备份
int _2;
int i = 1;
for (i = 1;;i++)
{
_2 = 1;
_2 <<= i;
//计算2^i
_2-=1;
j &=_2;
//计算j mod 2^i
_2 = 1;
//恢复
_2 <<= i-1;
//计算2^i-1
if ( j < _2)
{
int sigama = temp>>=i;
//计算σ
return &BinaryTree[sigama];
//在顺序存储序列中,N(m-i,σ)的序号为σ
}//end if
j = temp;
//将j还原
}//end for
} //end function
void main()
{
unsigned n;
//层数
unsigned k;
//点所在层
unsigned N=1; //树的节点数目
unsigned valid;
//标记用于过滤同层中无后续节点的节点
scanf("%d",&n);
N <<= n;
N-=1;
//计算节点的个数2^n
-1
Nodes *BiTreeArr;
//二叉数组指针
Nodes *Ans;
//用于保留返回值
BiTreeArr=(Nodes*)malloc(sizeof(Nodes)*N);//开辟空间
IniteTree(N,BiTreeArr);
//初始化二叉树顺序存储数组,里面存放了节点的坐标
for (unsigned i =2;i<N;i++)
{
valid = 1;
k= 1+ (int)(log10(i)/log10(2)); //计算节点所在的层
valid <<= k;
//valid=2^k,如果j==valid-1的话说明j在这层的最后,则在当层没有后续节点
if (i+1 != valid) //若为有效节点
{
Ans
= FindLCA(k, i,BiTreeArr);
printf("%d & %d ^=%d \n",i,i+1,Ans->x);
}//end if
}//end for
}//end main