题目链接

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. Ext常用开发基础知识

    Ext常用开发基础知识 组件定义 //这种方法可以缓存所需要的组件 调用起来比较方便(方法一 ) Ext.define('MySecurity.view.home.HomePanel', { //添加 ...

  2. 富文本编辑器嵌入指定html代码

    先把内容放入一个input中 <input id="detail" type="hidden" value="${sysCarousel.det ...

  3. SpringMvc多视图配置(jsp、velocity、freemarker) 在src目录views.properties配置

    #welcome为modelAndView.setViewName(" welcome " ) ; 中的welcome .(class)固定写法 welcome.(class)=o ...

  4. Educational Codeforces Round 21 A-E题题解

    A题      ............太水就不说了,贴下代码 #include<string> #include<iostream> #include<cstring& ...

  5. Python爬虫从入门到放弃(十三)之 Scrapy框架的命令行详解

    这篇文章主要是对的scrapy命令行使用的一个介绍 创建爬虫项目 scrapy startproject 项目名例子如下: localhost:spider zhaofan$ scrapy start ...

  6. 关于TRIM的优化技巧

    背景 今天在论坛中,看到有人在问一个千万级别表查询的优化.一个简单的查询几分钟.语句如下 SELECT  work_date ,        major ,        style ,      ...

  7. JavaScript中判断鼠标按键(event.button)

    <div id="test"></div> <script> document.oncontextmenu=function(){ return ...

  8. Vulkan Tutorial 29 Loading models

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Introduction 应用程序现在已经可以渲染纹理3D模型,但是 vertice ...

  9. HDU 6040---Hints of sd0061(STL)

    题目链接 Problem Description sd0061, the legend of Beihang University ACM-ICPC Team, retired last year l ...

  10. 【网络】dns_probe_finished_nxdomain 错误

    解决方案: 谷歌浏览器地址栏输入 chrome://net-internals/#dns 清除 dns 缓存即可