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. 穿戴-智能穿戴-ProjectGlass:谷歌眼镜(Google Project Glass)

    ylbtech-穿戴-智能穿戴-ProjectGlass:谷歌眼镜(Google Project Glass) 谷歌眼镜(Google Project Glass)是由谷歌公司于2012年4月发布的一 ...

  2. 深入RESTful无状态原则

    目录 目录 前言 无状态原则 Web服务的状态 基于状态的Web服务 基于无状态的Web服务 总结两者的区别 前言 在上篇RESTful基础知识中整体的介绍了RESTful架构设计思想的框架,在往后的 ...

  3. shell脚本一一项目4

    主题:一键查看服务器使用率 cpu vmstat  suyu wa memery free disk  df -h  /dev tcp连接数 netstat cpu(){ used=$(vmstat ...

  4. Vim 8.0 版本安装方法及添加Python支持

    利用Git安装 最简单也是最有效的方法 1. 获取Vim仓库: git clone https://github.com/vim/vim.git 2. 升级到最新的版本: cd vim git pul ...

  5. K-th Number 【POJ - 2104】【可持久化线段树】

    题目链接 因为这道题没有删除修改之类的,所以很多人会用离散化之后的线段树来做,但是实际上(可能是我懒得去做离散化这个操作了),然后就是直接写可持久化线段树,区间的长度就是int的从最小到最大的长度,然 ...

  6. Convolutional Neural Networks(4):Feature map size,Padding and Stride

    在CNN(1)中,我们用到下图来说明卷积之后feature maps尺寸和深度的变化.这一节中,我们讨论feature map size, padding and stride. 首先,在Layer1 ...

  7. CentOS防火墙命令集

    1. firewalld的基本使用 启动防火墙: systemctl start firewalld 防火墙状态: systemctl status firewalld 停止防火墙: systemct ...

  8. MyEclipse停止自带插件的启动

    MyEclipse启动时因为自身带有很多的插件,所以在启动时运行的速度特别慢,所以可以选择一下启动时的插件,将不使用的插件选择在MyEclipse启动时不起动. 步骤如下: windows->p ...

  9. Git相关命令整理

    git config --global user.name  //配置姓名git config --global user.email  //配置邮箱git config --list  //查看配置 ...

  10. MyEclipse创建maven项目时报: org.apache.maven.archiver.MavenArchiver.getManifest 错误

    创建项目报错,如图: 原因就是maven的配置文件不是最新的,MyEclipse2014解决方法: 1.help ->Install New sitie... 2.点击add 3.填写name和 ...