http://acm.hdu.edu.cn/showproblem.php?pid=6121

题意:
给你一颗完全K叉树,求出每棵子树的节点个数的异或和。

思路:

首先需要了解一些关于完全K叉树或满K叉树的一些知识:

对于每棵子树,只有三种情况:

①是满K叉树  ②不是满K叉树  ③叶子节点

并且非满K叉树最多只有一个,所以只需要将它进行特殊处理,具体看代码吧,说不清楚。代码参考了http://blog.csdn.net/my_sunshine26/article/details/77200282

当K=1时,树是链状的,需要打表找规律!

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; ll n, k, ans;
ll num[maxn];
int depth; void init()
{
num[]=;
for(int i=;i<=depth;i++)
{
num[i]=num[i-]+pow((long double)k,(long double)i-); //因为pow默认是pow(int,int),没有对应long long的,所以我这儿用long double来代替了一下
} //当然这里可以自己用快速幂来计算
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&n,&k);
if(k==) //特判
{
ll tmp=n%;
if(tmp==) ans=n;
else if(tmp==) ans=;
else if(tmp==) ans=n+;
else ans=;
printf("%I64d\n",ans);
continue;
} depth=;
ll tmp=n-;
//计算树的深度
while(tmp>)
{
tmp=(tmp-)/k;
depth++;
}
init(); //预处理前i层的节点个数
ans=;
ans^=(n-num[depth-])&; //先处理一下最后一层
depth--;
ll now=; //当前从下往上第几层
ll pos=(n--)/k;
while(depth>)
{
ll left=num[depth-]; //当前层数最左边编号
ll right=num[depth]-; //当前层数最右边编号
ll tmp1=num[now]; //左边树大小
ll tmp2=num[now-]; //右边树大小 //奇数才有贡献
if((pos-left)&) ans^=tmp1;
if((right-pos)&) ans^=tmp2; ll cnt=pos;
while(cnt<=(n--)/k) cnt=cnt*k+;
ans^=(num[now-]+n-cnt); //单独处理临界点子树
now++;
depth--;
pos=(pos-)/k; }
printf("%I64d\n",ans);
}
return ;
}

HDU 6121 Build a tree(完全K叉树)的更多相关文章

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

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

  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 —— 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 ...

  5. hdu 6121 Build a tree

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

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

  7. 【2017 Multi-University Training Contest - Team 7 && hdu 6121】Build a tree

    [链接]点击打开链接 [题意] 询问n个点的完全k叉树,所有子树节点个数的异或总和为多少. [题解] 考虑如下的一棵k=3叉树,假设这棵树恰好有n个节点. 因为满的k叉树,第i层的节点个数为k^(i- ...

  8. 【最优K叉树】hdu 5884 Sort

    http://acm.hdu.edu.cn/showproblem.php?pid=5884 参考:https://www.cnblogs.com/jhz033/p/5879452.html [题意] ...

  9. 【满k叉树】Perfect Tree

    题目描述 Given a positive integer k, we define a rooted tree to be k-perfect, if and only if it meets bo ...

随机推荐

  1. XMind思维导图自定义图标/图片

  2. 001-nginx基础配置-location

    一.基础语法 Location block 的基本语法形式是: location [=|~|~*|^~|@] pattern { ... } [=|~|~*|^~|@] 被称作 location mo ...

  3. break continue练习

    break :跳出 1.当break单独存在时,下面不要定义其他语句,因为执行不到. 2.如果出现了循环嵌套,break响应跳出指定的循环,可以通过标号来完成 例如: continue:要么是swit ...

  4. git 生成patch 和打入patch

    转载:https://blog.csdn.net/liuhaomatou/article/details/54410361 平时我们在使用git 管理项目的时候,会遇到这样一种情况,那就是客户使用gi ...

  5. angular.element 动态添加和删除元素

    addClass()-为每个匹配的元素添加指定的样式类名after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点append()-在每个匹配元素里面的末尾处插入参数内容att ...

  6. Razor中的@:和语法

    用Razor实现流畅编程 Razor尽量减少编写一个视图模板需要敲入的字符数,实现快速流畅的编程工作流.与大部分模板的语法不同,你不必在HTML中为了明确地标记出服务模块 的开始和结束而中断编程.Ra ...

  7. [LeetCode] 198. House Robber _Easy tag: Dynamic Programming

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. NLP总览

    一.自然语言处理概述 1)自然语言处理:利用计算机为工具,对书面实行或者口头形式进行各种各样的处理和加工的技术,是研究人与人交际中以及人与计算机交际中的演员问题的一门学科,是人工智能的主要内容. 2) ...

  9. python直接赋值、浅拷贝和深拷贝

    # 解: # import copy # names1=['Amir','Barry','Cgakes','Dao',[11,22,33]] # names2=names1#直接赋值,指向同一个对象 ...

  10. #C++初学记录(算法4)

    A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...