HDU 6121 Build a tree —— 2017 Multi-University Training 7
. 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的更多相关文章
- 2017多校第7场 HDU 6121 Build a tree K叉树,思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:一个n个点的完全k叉树,求每个节点的size的异或和. 解法:容易发现,考虑根的所有孩子, ...
- HDU 6121 Build a tree(找规律+模拟)
Build a tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- HDU 6121 Build a tree(完全K叉树)
http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:给你一颗完全K叉树,求出每棵子树的节点个数的异或和. 思路: 首先需要了解一些关于完全K叉树或满K叉 ...
- 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− ...
- HDU 6121 Build a tree(k叉树的子树大小相异)
http://acm.hdu.edu.cn/showproblem.php?pid=6121 题目大意: 给你一颗 n 个节点的完全 k 叉树,问你这棵树中所有子树结点个数的总异或值. 分析: 我们很 ...
- hdu 6121 Build a tree
/** * 题意:一棵 n 个点的完全 k 叉树,结点标号从 0 到 n - 1,求以每一棵子树的大小的异或和. * 解法:k叉树,当k=1时,特判,用xorn函数,具体解释:http://blog. ...
- 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两节 ...
- 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 ...
- HDU 6170 - Two strings | 2017 ZJUT Multi-University Training 9
/* HDU 6170 - Two strings [ DP ] | 2017 ZJUT Multi-University Training 9 题意: 定义*可以匹配任意长度,.可以匹配任意字符,问 ...
随机推荐
- HTML 浅层漫谈
讲到HTML最主要的就是如何给这个网页搭架子给他大致的把这个人物样子先慢慢画出来,然后我们后面去学习这个CSS还有JS的时候给这个雏形慢慢的去加一些东西,让你的网页变得越来越好看 1.标签的使用 a: ...
- 【ABAP系列】SAP ABAP 工单增强
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 工单增强 ...
- 【Unity 系统知识】 各种路径
一.Assets下的Resources(Unity系统文件夹) :路径 Application.dataPath/Resources 可以使用Resources.Load("文件名字,注:不 ...
- Convolutional Neural Networks(1): Architecture
Concolutional Neural Networks(CNN)同样使用三层结构,但结构上同Feedforward Neural Network有很大不同,其结构如下图: Input layer: ...
- SQLServer2008查询时对象名无效
情况一:如果表名是关键字,查询时把表名括起来,不作为关键字使用 情况二:看左上角显示的是否是master,这是数据库的默认系统库,点选这个改成自己的即可
- 【小刘的linux学习笔记 】——01认识操作系统
1.操作系统的地位 计算机系统由硬件和软件两部分组成.通常把未配置软件的计算机称为裸机.直接使用裸机不仅不方便,而且将严重降低工作效率和机器的利用率. 操作系统(OS,Operation System ...
- 转 Python selenium 强制等待显示等待隐式等待
1. 1. 强制等待第一种也是最简单粗暴的一种办法就是强制等待sleep(xx),强制让闪电侠等xx时间,不管凹凸曼能不能跟上速度,还是已经提前到了,都必须等xx时间. 看代码: # -*- codi ...
- java8新特性-简介
一.主要内容 :其中最为核心的为lambda 表达式 与 Stream API lambda表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期API ...
- django的配置
1.django的默认配置 import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) # 获取 ...
- 11、numpy——字符串函数
NumPy 字符串函数 以下函数用于对 dtype 为 numpy.string_ 或 numpy.unicode_ 的数组执行向量化字符串操作. 它们基于 Python 内置库中的标准字符串函数. ...