HazelFan wants to build a rooted tree. The tree has nn nodes labeled 0 to n−1, and the father of the node labeled i is the node labeled 

. HazelFan wonders the size of every subtree, and you just need to tell him the XOR value of these answers.


Input

The first line contains a positive integer T(1≤T≤5)T(1≤T≤5), denoting the number of test cases. 
For each test case: 
A single line contains two positive integers n,k(1≤n,k≤1018)n,k(1≤n,k≤1018). 
Output

For each test case: 
A single line contains a nonnegative integer, denoting the answer.Sample Input

2
5 2
5 3

Sample Output

7
6 题意:画一下题目介绍的树,发现这是一个k叉树,那么题目要求所有子树大小的异或和。
思路:我们从序号为n的节点开始,往上求异或和。n计算到某一层时,对于处在n节点左边的节点(子树),它们是t+1层的满k叉树,在右边的节点(子树)是t层的满二叉树,
满二叉树的大小可以通过打表得到。根据异或计算的性质,大小相同的子树进行异或,有奇数个进行异或时结果为该子树的大小,偶数个时为0。
而对n位置节点的子树来说,不是满二叉树就是非满二叉树,而且在计算这个子树大小时一旦它在某一层是非满二叉树,接下来往上它就一直是非满二叉树。
还有,k==1时特判一下。
具体实现参看代码。 AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long LL;
LL n, k;
LL num[];
int main()
{
int T;
cin>>T;
num[]=;
while(T--)
{
scanf("%lld %lld", &n, &k);
if(k==){
if(n==)
printf("%d\n", );
else if(n==)
printf("%d\n",);
else if(n==)
printf("%d\n", );
else
{
int k=n%;
if(k==)
printf("%lld\n", n);
else if(k==)
printf("%d\n", );
else if(k==)
printf("%lld\n", n+);
else
printf("%d\n", );
}
continue;
}
n--;
int level=,t=;
LL lm=, rm=;
while(rm<n){
t++;
lm=rm+;
rm=lm*k;
num[t]=rm+;
}
LL res=,left,right,s=n+,m;
t=;
bool flag=;
LL mid=n%k;//mid判断是否为满k叉树, m为非满k叉树的大小
left=n-mid-lm+;
//cout<<left<<endl;
if(left&){
res^=num[];
}
if(mid&)
res^=num[];
if(mid)
flag=; m=(n-(n-)/k*k)*num[]+;
//cout<<m<<endl;
while(n)
{ //cout<<res<<'*'<<endl; t++;
//cout<<t<<' '<<m<<endl; n=(n-)/k;
if(n<=)
break; mid=n%k;
lm=(lm-)/k;
rm=(rm-)/k;
//cout<<lm<<' '<<n<<' '<<rm<<endl;
right=rm-n;
left=n-lm+;
if(flag||mid){
flag=;
left--;
res^=m;
} if(left&)
res^=num[t];
if(right&)
res^=num[t-];
m+=(n-(n-)/k*k-)*num[t]+(((n-)/k+)*k-n)*num[t-]+; }
res^=s;
printf("%lld\n", res);
}
return ;
}

												

HDU 6121 Build a tree —— 2017 Multi-University Training 7的更多相关文章

  1. 2017多校第7场 HDU 6121 Build a tree K叉树,思维

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:一个n个点的完全k叉树,求每个节点的size的异或和. 解法:容易发现,考虑根的所有孩子, ...

  2. HDU 6121 Build a tree(找规律+模拟)

    Build a tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)To ...

  3. HDU 6121 Build a tree(完全K叉树)

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:给你一颗完全K叉树,求出每棵子树的节点个数的异或和. 思路: 首先需要了解一些关于完全K叉树或满K叉 ...

  4. 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)

    题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...

  5. HDU 6121 Build a tree(k叉树的子树大小相异)

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 题目大意: 给你一颗 n 个节点的完全 k 叉树,问你这棵树中所有子树结点个数的总异或值. 分析: 我们很 ...

  6. hdu 6121 Build a tree

    /** * 题意:一棵 n 个点的完全 k 叉树,结点标号从 0 到 n - 1,求以每一棵子树的大小的异或和. * 解法:k叉树,当k=1时,特判,用xorn函数,具体解释:http://blog. ...

  7. HDU 6162 - Ch’s gift | 2017 ZJUT Multi-University Training 9

    /* HDU 6162 - Ch’s gift [ LCA,线段树 ] | 2017 ZJUT Multi-University Training 9 题意: N节点的树,Q组询问 每次询问s,t两节 ...

  8. hdu 6241 Color a Tree 2017 CCPC 哈理工站 L

    Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are ...

  9. HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9

    /* HDU 6170 - Two strings [ DP ] | 2017 ZJUT Multi-University Training 9 题意: 定义*可以匹配任意长度,.可以匹配任意字符,问 ...

随机推荐

  1. 彻底理解 Linux 的搜索工具: grep 和 awk

    grep 官方手册 awk 官方手册, awk 学习资料 1. grep grep 用于打印匹配指定模式的行. 1.1 介绍 grep 命令从输入文件中查找匹配到给定模式列表的行.发现匹配到的行后,默 ...

  2. 设置chrome解决跨域问题

    开发中遇到跨域问题, 解决方法一 以关闭安全验证模式启动chrome,就不会报跨域了  开发阶段,用这个方式是可以的,不然他需要后台配置成 * , 发布会有风险  设置方法:  --disable-w ...

  3. 笨方法学Python 错误记录

    ex8:忘记输入“空格”ex9:忘记输入“冒号”ex14:%前后要空格,否则errorex21:多个函数嵌套,漏写括号)ex24:%d,漏写d,导致程序错误:"""之间的 ...

  4. 基于K-means Clustering聚类算法对电商商户进行级别划分(含Octave仿真)

    在从事电商做频道运营时,每到关键时间节点,大促前,季度末等等,我们要做的一件事情就是品牌池打分,更新所有店铺的等级.例如,所以的商户分入SKA,KA,普通店铺,新店铺这4个级别,对于不同级别的商户,会 ...

  5. 23.协程的使用场景,高I/O密集型程序

    import time def one_hundred_millionA(): start_time = time.time() i = 0 for _ in range(100000000): i ...

  6. Learning OSG programing---Multi Camera in one window 在单窗口中创建多相机

    在学习OSG提供的例子osgCamera中,由于例子很长,涉及很多细节,考虑将其分解为几个小例子.本文介绍实现在一个窗口中添加多个相机的功能. 此函数接受一个Viewer引用类型参数,设置图形上下文的 ...

  7. JavaScript Sort

    function ArrayList() { var array = []; this.swap = function(index1, index2) { var aux = array[index1 ...

  8. spring boot 控制 序列化格式

    @PostMapping(path = "/register", produces={MediaType.APPLICATION_JSON_VALUE, MediaType.TEX ...

  9. CompletionService的好处与使用场景

    转自:https://blog.csdn.net/jdsjlzx/article/details/52912701 FutureTask既是Future.Runnable,又是包装了Callable( ...

  10. canvas添加事件

    https://blog.csdn.net/xundh/article/details/78722744