题目链接

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. 简析Android 兼容性测试框架CTS使用

    一.什么是兼容性测试? 1)为用户提供最好的用户体验,让更多高质量的APP可以顺利的运行在此平台上 2)让程序员能为此平台写更多的高质量的应用程序 3)可以更好的利用Android应用市场 二.CTS ...

  2. 致命错误:mysql/cli 目录 #include "mysql/client_plugin.h"

    居然说没有mysql.h这个文件,可是我确实安装了mysql了啊.......  原来是缺少libmysqlclient-dev,OK安装就是了 ubuntu下  :  audo apt-get in ...

  3. Java分形

    目前笔者接触过的分形主要有一下几种: 1.类似Clifford的分形.这种分形的特点是:分形的初始坐标为(0,0),通过初始坐标经过大量的迭代,得到一系列的点,根据得到的点来绘制分形曲线.这类分形的参 ...

  4. opencv如何实现【不用全局变量进行滚动条控制】

    opencv中自带滚动条,其中一个问题是该回调函数(on_trackbar)大多使用的是全局变量,大型项目调试时弊端众多,比如下图: 为此,留意了void on_Trackbar(int par1, ...

  5. mysql主从复制原理探索

    上一篇文章里面,讲到了遇到mysql主从延迟的坑,对于这次的坑多说两句,以前也看过这样的例子,也知道不能够写完之后马上更新,但是真正开发的时候还是没有注意到这一点,道理大家都懂,但是还是会犯错,只有等 ...

  6. java IO之 File类+字节流 (输入输出 缓冲流 异常处理)

    1. File类

  7. C# Using的用法

    C#里面Using有两种用法: 1.作为指令. using+命名空间,导入其他命名空间中定义的类型,这样可以在程序中直接用命名空间中的类型,不必指定命名空间: 命名空间是.NET程序在逻辑上的组织结构 ...

  8. 使用curl,libcurl访问Https

    编译curl,libcurl 下载curl源码(git clone https://github.com/curl/curl),在目录curl\winbuild\BUILD.WINDOWS.txt文件 ...

  9. Java设计模式之模板方法设计模式(银行计息案例)

    不知道为什么,这几天对Java中的设计模式非常感兴趣,恰巧呢这几天公司的开发任务还不算太多,趁着有时间昨天又把模板方法模式深入学习了一下,做了一个客户在不同银行计息的小案例,感触颇深,今天给各位分享一 ...

  10. (转)如何在maven的pom.xml中添加本地jar包

    1 maven本地仓库认识 maven本地仓库中的jar目录一般分为三层:图中的1 2 3分别如下所示: 1 groupId 2 artifactId 3 version 4 jar包的依赖 如果要将 ...