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−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
题意:
有一个由n个节点(编号为0~n-1)构成的完全k次树,节点i的父节点的编号为 ⌊(i−1)/k⌋。求出所有的节点大小的异或值。
分析:
因为这是一棵完全k次树,我们手下可以确定的一点就是如果这是一棵满k次树,那么所有的叶子节点都在最后一层,否则除最后一层不满外,倒数第二成还有一部分的叶子结点,也就是说所有叶子结点的高度差最多为1,而且最后一层的叶子结点还全部在左边,倒数第二成的叶子结点全部在右边.
还可以发现或许会存在(如果存在的话仅有一个)有一个节点有子节点,但是子节点的个数又不是k,我们称这个节点为残缺节点。
最后一层的残缺节点肯定就是最后一个节点,我们可以发现该层上它左边节点的大小全部为1,右边全部为0,在往上递归回溯的时候,这时当前节点子树的大小特殊,其左边的所有同层次节点子树大小相同,其右边的所有同层次节点子树大小相同,所以对于每一层只需要考虑三种不同的节点子树大小以及个数。
具体的解释参考代码,注释很详细。
代码:
#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long LL;
const LL N=1e18+5;
LL cnt[70];///每一层的残缺节点所在的地方
LL pw[70];///每一层的所有的节点的个数
int deep;
LL n,k,m,ans,L,R,mid;
void dfs(int x)
{
/*
L:左兄弟节点往下的所有节点的个数
R:右兄弟节点往下的所有节点的个数
mid:残缺节点往下的所有的节点的个数
*/
if(x>deep) return ;
dfs(x+1);
LL pos=(cnt[x]-1)%k; ///pos表示的是于那个残缺节点同父节点的左边的兄弟节点个数
int f=(cnt[x]-1)&1;///左边的兄弟节点个数的奇偶性,奇数才异或一次
if(f) ans^=L;
f=(pw[x]-cnt[x])&1;///同理右边的子树个数的奇偶性
if(f) ans^=R;
ans^=mid;
mid=pos*L+mid+1+(k-pos-1)*R;
L=L*k+1;///左边一个子树上的节点个数
R=R*k+1;///右边一个子树上的节点个数
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%lld%lld",&n,&k);
if(k == 1)///为1的情况单独考虑
{
if(n%4 == 0) ans = n;
else if(n%4 == 1) ans = 1;
else if(n%4 == 2) ans = n+1;
else if(n%4 == 3) ans = 0;
printf("%lld\n",ans);
continue;
}
LL tmp=1;
m=n-1;
pw[0]=1;///pw表示的是每一层的节点个数
for(int i=1; i<70; i++)
{
tmp=tmp*k;
pw[i]=tmp;
if(m<tmp || tmp<0 )
{
pw[i]=N;
deep=i;
break;
}
m-=tmp;
}
cnt[deep]=m;///最后一层不满有m个
if(m==0)///最后一层为0个的话,相当于上一层正好铺满往上移一层
{
deep--;
m=cnt[deep]=pw[deep];
}
///之后的每一层
for(int i=deep-1; i>=0; i--)
{
cnt[i]=(m+k-1)/k;
m=cnt[i];
}
L=1;
mid=1;
R=0;
ans=0;
dfs(0);
printf("%lld\n",ans);
}
return 0;
}
2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)的更多相关文章
- 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)
题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...
- 2017ACM暑期多校联合训练 - Team 6 1002 HDU 6097 Mindis (数学)
题目链接 Problem Description The center coordinate of the circle C is O, the coordinate of O is (0,0) , ...
- 2017ACM暑期多校联合训练 - Team 1 1002 HDU 6034 Balala Power! (字符串处理)
题目链接 Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He ...
- 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)
题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...
- 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)
题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...
- 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)
题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
- 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)
题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...
- 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)
题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...
随机推荐
- 对象库(UI MAP)
目的:能够使用配置文件存储被测页面上元素的定位方式和定位表达式,做到定位数据和程序的分离. 测试程序写好以后,可以方便不具备编码能力的测试人员进行自定义修改和配置 : package dataDriv ...
- 第94天:CSS3 盒模型详解
CSS3盒模型详解 盒模型设定为border-box时 width = border + padding + content 盒模型设定为content-box时 width = content所谓定 ...
- BZOJ 2152 聪聪可可(树形DP)
给出一颗n个点带边权的树(n<=20000),求随机选择两个点,使得它们之间的路径边权是3的倍数的概率是多少. 首先总的对数是n*n,那么只需要统计路径边权是3的倍数的点对数量就行了. 考虑将无 ...
- BZOJ 1483 梦幻布丁(链表+启发式合并)
给出一个长度为n的序列.支持两种操作: 1.把全部值为x的修改成y.2.询问序列有多少连续段. 我们可以对于每个值建立一个链表.对于操作1,则可以将两个链表合并. 对于操作2,只需要在每次合并链表的时 ...
- 【bzoj5206】[Jsoi2017]原力 根号分治+STL-map
题目描述 一个原力网络可以看成是一个可能存在重边但没有自环的无向图.每条边有一种属性和一个权值.属性可能是R.G.B三种当中的一种,代表这条边上原力的类型.权值是一个正整数,代表这条边上的原力强度.原 ...
- android面试(3)---基本问题
1.值类型,引用类型? 基本数据类型都是值类型:byte,short,int,long,float,double,char,boolean 其他类型都是引用类型. 引用类型在传入方法是,方法内部对引用 ...
- 【JavaScript&jQuery】省市区三级联动
HTML: <%@page import="com.mysql.jdbc.Connection"%> <%@ page language="java&q ...
- C++ Win系统下的调试
有的时候我们找不出错误在哪里,这时候我们需要调试一遍看看到底是哪里出了问题:我们需要分布查看程序运行情况. 这时候我们用到了调试这样一个神奇的东西. 一.基于Dev cpp环境下的调试 Dev cpp ...
- Spring切面之一
为什么要使用AOP,在编写程序的时候,除了不必关心依赖的组件如何实现,在实际开发过程中,还需要将程序中涉及的公共问题集中解决.AOP是Aspect-Oriented Programming的简称,意思 ...
- 全面支持Angular2的Web后台Bootstrap模板Sing App - Web & Angular 2.0 Dashboard
在线预览 Sing App v3.3.0 (包含Angular 2.0版本实现) 现在,本模板完全支持Angular2.0版本啦. Sing Web App 是由专业前端工程师采用行业内流行的技术构建 ...