题目链接

Problem Description
HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n−1, and the father of the node labeled i is the node labeled ⌊i−1k⌋. 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), denoting the number of test cases.
For each test case:
A single line contains two positive integers 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
 
 
题意:有一颗树,节点编号0~n-1 ,节点 i 的父亲节点编号 ⌊i−1k⌋ ,求所有子树大小相异或值?
 
思路:可以发现这是一棵完全 k 叉树 ,那么所有叶子节点高度差最多为1,且所有最高层叶子都靠左。那么我们从上向下找最高最靠右的叶子,然后回溯时计算:这时当前节点子树的大小特殊,其左边的所有同层次节点子树大小相同,其右边的所有同层次节点子树大小相同,所以对于每一层只需要考虑三种不同的节点子树。
 
         官方题解:
       
 
 

代码如下:(唉,比赛时代码没调出来,赛后才调完,有点可惜~)

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const LL N=1e18+;
LL cnt[];
LL pw[];
LL n,k,m;
int deep;
LL ans=;
LL L,R,mid; void dfs(int x)
{
if(x>deep) return ;
dfs(x+);
LL pos=(cnt[x]-)%k; ///left brother; int f=(cnt[x]-)&;
if(f) ans^=L; LL cR=pw[x]-cnt[x];
f=cR&;
if(f) ans^=R; ans^=mid; mid=pos*L+mid++(k-pos-)*R;
L=L*k+;
R=R*k+;
} int main()
{
int T; cin>>T;
while(T--)
{
scanf("%lld%lld",&n,&k);
if(k == ){
if(n% == ) ans = n;
else if(n% == ) ans = ;
else if(n% == ) ans = n+;
else if(n% == ) ans = ;
printf("%lld\n",ans);
continue;
}
LL tmp=;
m=n-; pw[]=;
for(int i=;i<;i++)
{
tmp=tmp*k; pw[i]=tmp;
if(m<tmp || tmp< ) { pw[i]=N; deep=i; break; }
m-=tmp;
}
cnt[deep]=m;
if(m==) { deep--; cnt[deep]=pw[deep]; m=cnt[deep]; }
for(int i=deep-;i>=;i--)
{
cnt[i]=(m+k-)/k;
m=cnt[i];
}
L=; mid=; R=;
ans=;
dfs();
printf("%lld\n",ans);
}
return ;
}

hdu 6121---Build a tree(深搜+思维)的更多相关文章

  1. 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− ...

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

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

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

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

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

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

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

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

  6. HDU 6121 Build a tree —— 2017 Multi-University Training 7

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

  7. hdu 6121 Build a tree

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

  8. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  9. HDU 2553 N皇后问题(深搜DFS)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. JQuery实现点击关注和取消功能

    点赞,网络用语,表示“赞同”.“喜爱”. 该网络语来源于网络社区的“赞”功能.送出和收获的赞的多少.赞的给予偏好等,在某种程度能反映出你是怎样的人以及处于何种状态.点赞的背后,反映出你自己.与之对应的 ...

  2. Java基础----jdk1.8 反射实验

    (写在最前:还没入门的搬砖工的一本正经的胡说八道) 引言:  最近做到的项目中,需要给对接方提供一个公共接口,根据对方传入的XML文件的rootelement分发调用接口,最简单的使用if-else ...

  3. 网络编程学习笔记(三)UDP

    1.不可靠的.效率高.数据报(数据打成一个小包一个小包地往外发).非连接. 2.UDP是非连接的,因此严格来说并不区分服务器端和客户端. 3.UDP通信过程:UDP都是通过字节数组进行对话的.     ...

  4. [图形学] 习题8.6 线段旋转后使用Cohen-Sutherland算法裁剪

    习题8.6 生成一条比观察窗口对角线还长的线段动画,线段重点位于观察窗口中心,每一帧的线段在上一帧基础上顺时针旋转一点,旋转后用Cohen-Sutherland线段裁剪算法进行裁剪. 步骤: 1 视口 ...

  5. 二、redis集群搭建

    redis集群搭建 redis3.0后支持集群.集群中应该至少有三个节点,每个节点有一备份节点.需要6台服务器.搭建伪分布式,需要6个redis实例.搭建集群的步骤: 一.安装单机版redis 第一步 ...

  6. 利用base64函数,对文件进行转码加密

    设计此种编码是为了使二进制数据可以通过非纯 8-bit 的传输层传输,例如电子邮件的内容就是通过base64转码后传输的.Base64-encoded后, 数据要比原始数据多占用 33% 左右的空间. ...

  7. OpenStack neutron 环境云主机使用keepalived vip + 给vip绑定浮动IP 步骤及注意事项

    在openstack环境创建的多台云主机配置keepalived作主备,默认情况下无法生效,直接对云主机一张网卡配置两个IP进行测试也是同样结果,因为: 可以看到,port所在的宿主机上iptable ...

  8. ubuntu14.04_caffe2安装

    今天F8开发者大会上,Facebook正式发布Caffe2.经过一天的折腾,终于在ubuntu14.04上成功配置caffe2,现将经验分享如下: 1.ubuntu14.04系统下caffe2所需依赖 ...

  9. 51nod_1661: 黑板上的游戏(sg函数 找规律)

    题目链接 先打一个sg函数的表,找找规律,发现sg函数可以递归求解 打表代码如下 #include<bits/stdc++.h> using namespace std; typedef ...

  10. redis分布式锁和消息队列

    最近博主在看redis的时候发现了两种redis使用方式,与之前redis作为缓存不同,利用的是redis可设置key的有效时间和redis的BRPOP命令. 分布式锁 由于目前一些编程语言,如PHP ...