/**
* 题意:一棵 n 个点的完全 k 叉树,结点标号从 0 到 n - 1,求以每一棵子树的大小的异或和。
* 解法:k叉树,当k=1时,特判,用xorn函数,具体解释:http://blog.csdn.net/a3630623/article/details/12371727
* k不等一1;我们dfs求解,当是满k叉树,可以很快求的;每层最对可能三类树,少一层的满k叉树,
* 少两层的满k叉树,不满的子树dfs求解。
*/
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;
const int INF=2e9+1e8; const int MOD=1e9+7;
const double eps=0.0000000001;
void fre()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
}
#define MSET(a,b) memset(a,b,sizeof(a)) const int maxn=1e6+10;
LL ans;
LL xorn(LL n) //1~n连续异或的值;
{
LL t = n & 3ll;
if (t & 1ll)
return t / 2ll ^ 1ll;
return t / 2ll ^ n;
}
LL xorpow(LL a,LL b) // b个a连续异或
{
if(b%2==0) return 0;
else return a;
}
LL getnn(LL n,LL k) //n层k叉树的加点个数
{
LL res=0,t=1;
while(n--)
{
res+=t;
t*=k;
}
return res;
}
LL man(LL n,LL k) //n层满k叉树的异或值;
{
if(n<=0) return 0;
LL res=0,sz=getnn(n,k),t=1;
for(LL i=1;i<=n;i++)
{
res^=xorpow(sz,t);
sz=(sz-1)/k;
t*=k;
}
return res;
}
void dfs(LL n, LL k)
{
if(n==0) return ;
LL deep = 0;
for (LL m = n,t=1;m>0; t*=k) //得到深度
{
m-=t;
deep++;
}
if(getnn(deep,k)==n) //判断是否是满k叉树?如果是直接得出答案;
{
ans^=man(deep,k);
return ;
}
else
{
if(deep<=2) //深度为2,不用在递归了,到头了,直接求异或
{
ans^=n;
ans^=xorpow(1ll,n-1);
return ;
}
else
{
ans ^= n;
LL tp=n-getnn(deep-1,k);
for(LL i=1;i<=deep-2;i++) tp/=k;
ans^=(xorpow(man(deep-1,k),tp));//深度-1的满k叉树
ans^=(xorpow(man(deep-2,k),k-tp-1));//深度-2 的满k叉树
dfs(n-1-tp*getnn(deep-1,k)-getnn(deep-2,k)*(k-1-tp),k); //剩下的子树,dfs求;
}
}
} int main()
{
int ncase;
scanf("%d",&ncase);
while(ncase--)
{
LL n,k;
scanf("%lld%lld",&n,&k);
if(k==1)
{
printf("%lld\n",xorn(n));
}
else
{
ans=0;
dfs(n,k);
printf("%lld\n",ans);
}
}
return 0;
} /**************************************************/
/** Copyright Notice **/
/** writer: wurong **/
/** school: nyist **/
/** blog : http://www.cnblogs.com/coded-ream/ **/
/**************************************************/ /** 1000000000000000000 1000000000000000000 */

最新文章

  1. SecureCRT使用技巧
  2. [转]Web Service Authentication
  3. iOS开发——UI篇Swift篇&amp;玩转UItableView(二)高级功能
  4. KMeans聚类算法Hadoop实现
  5. linux aio
  6. SQL Server 查看identity值的几种方法。
  7. Linux学习之域名解析命令
  8. jsp静态化之简单介绍
  9. angular2教程
  10. .net core 部署 centos7 初试
  11. LeetCode 11. Container With Most Water (装最多水的容器)
  12. 【CJOJ2499】【DP合集】棋盘 chess
  13. WebViewClient 与 WebChromeClient
  14. js中的运算符优先级
  15. javascript 闭包内部机制
  16. 以太坊 web3.js 文档翻译及说明
  17. Vue之单文件组件的数据传递,axios请求数据及路由router
  18. 揭开webRTC媒体服务器的神秘面纱——WebRTC媒体服务器&amp;开源项目介绍
  19. (笔记)Linux延时及时间函数总结
  20. 一个简单例子弄懂什么是javascript函数劫持

热门文章

  1. TCP/IP详解 卷一(第六章 ICMP:Internet控制报文协议)
  2. struts2获取前台提交的参数
  3. javascript回调函数,闭包作用域,call,apply函数解决this的作用域问题
  4. 统一建模语言(UML,Unified Modeling Language)
  5. iTunes备份注意
  6. 利用DataSet部分功能实现网站登录
  7. maven打包时无法加载lib下的jar
  8. ORACLE物化视图具体解释
  9. linux 经常使用命令
  10. sigar 监控服务器硬件信息