HDU 6121 Build a tree(完全K叉树)
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叉树)的更多相关文章
- HDU 6121 Build a tree(k叉树的子树大小相异)
http://acm.hdu.edu.cn/showproblem.php?pid=6121 题目大意: 给你一颗 n 个节点的完全 k 叉树,问你这棵树中所有子树结点个数的总异或值. 分析: 我们很 ...
- 2017多校第7场 HDU 6121 Build a tree K叉树,思维
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:一个n个点的完全k叉树,求每个节点的size的异或和. 解法:容易发现,考虑根的所有孩子, ...
- HDU 6121 Build a tree(找规律+模拟)
Build a tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- 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 ...
- hdu 6121 Build a tree
/** * 题意:一棵 n 个点的完全 k 叉树,结点标号从 0 到 n - 1,求以每一棵子树的大小的异或和. * 解法:k叉树,当k=1时,特判,用xorn函数,具体解释:http://blog. ...
- 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− ...
- 【2017 Multi-University Training Contest - Team 7 && hdu 6121】Build a tree
[链接]点击打开链接 [题意] 询问n个点的完全k叉树,所有子树节点个数的异或总和为多少. [题解] 考虑如下的一棵k=3叉树,假设这棵树恰好有n个节点. 因为满的k叉树,第i层的节点个数为k^(i- ...
- 【最优K叉树】hdu 5884 Sort
http://acm.hdu.edu.cn/showproblem.php?pid=5884 参考:https://www.cnblogs.com/jhz033/p/5879452.html [题意] ...
- 【满k叉树】Perfect Tree
题目描述 Given a positive integer k, we define a rooted tree to be k-perfect, if and only if it meets bo ...
随机推荐
- CentOS工作内容(三)配置网络IP地址
CentOS工作内容(三)配置网络IP地址 用到的快捷键 tab 自动补齐(有不知道的吗) ctrl+a 移动到当前行的开头(a ahead) ctrl+u 删除(剪切)此处至开始所有内容 vim 末 ...
- NYOJ-1073 最小值
http://acm.nyist.net/JudgeOnline/problem.php?pid=1073 # include<stdio.h> # include<stdlib.h ...
- Spark Streaming性能优化: 如何在生产环境下应对流数据峰值巨变
1.为什么引入Backpressure 默认情况下,Spark Streaming通过Receiver以生产者生产数据的速率接收数据,计算过程中会出现batch processing time > ...
- PAT World Cup Betting[非常简单]
1011 World Cup Betting (20)(20 分) With the 2010 FIFA World Cup running, football fans the world over ...
- openstack 部署笔记--glance
控制节点 创建用户及服务 $ . admin-openrc $ openstack user create --domain default --password-prompt glance $ op ...
- selenium python 启动Chrome
启动Chrom浏览器 下载chromedriver: http://chromedriver.storage.googleapis.com/index.html 当时找chromedriver与chr ...
- pycharm + git 的集成使用
1. 下载git 和Pycharm并安装 2. 打开Pycharm, 点击 file-->Default Settins-->Version Control-->Git 然后在 P ...
- Locust性能测试3-no-web模式和csv报告保存
前言 前面是在web页面操作,需要手动的点start启动,结束的时候也需要手工去点stop,没法自定义运行时间,这就不太方便. locust提供了命令行运行的方法,不启动web页面也能运行,这就是no ...
- PostgreSQL远程连接配置
postgresql默认情况下,远程访问不能成功,如果需要允许远程访问,需要修改两个配置文件,说明如下: 1.postgresql.conf 将该文件中的listen_addresses项值设定为“* ...
- VUE滚动条插件——vue-happy-scroll
最近自己在自学vue2.0,然后就自己摸索做一个简单的后台管理系统,在做的过程中,总感觉不同浏览器自带的滚动条样式不统一,也很难看,所以就在网上找一些使用vue的滚动条插件.最开始用的是Easy-sc ...