Build a tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 946    Accepted Submission(s): 369

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
 
Source
 
/*
* @Author: lyuc
* @Date: 2017-08-15 14:04:25
* @Last Modified by: lyuc
* @Last Modified time: 2017-08-16 22:11:31
*/ /*
题意:给你一个n个节点的k叉树,然后让你求每个子树节点个数的异或和 思路:
当k等于1的时候处理会超时的,但是有规律:
n%4=0 结果为n
n%4=1 结果为1
n%4=2 结果为n+1
n%4=3 结果为0
当n<=k+1时:
如果 n%2==1
结果 n 如果 n%2==0
结果 n+1
如果是个完全k叉树:
如果 k%2==0
结果 n 如果 k%2==1
结果 每层异或一个
剩余的情况中:
root节点的子树中最多只有一个子树是不完全k叉树,这棵树单独处理,然后剩下的是满k叉树
按照上面的办法求 */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> #define LL long long
#define INF 0x3f3f3f3f
#define MAXN 105 using namespace std; int t;
LL n,k;
LL res;
LL num[MAXN]; inline void init(){
res=;
} int main(){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
scanf("%d",&t);
while(t--){
init();
scanf("%lld%lld",&n,&k);
if(n<=k+){//如果是矩阵上三角
if(n%==){
printf("%lld\n",n);
}else{
printf("%lld\n",n+);
}
}else{
if(k==){//k=1的时候需要特殊处理
switch(n%){
case :
printf("%lld\n",n);
break;
case :
puts("");
break;
case :
printf("%lld\n",n+);
break;
case :
puts("");
break;
}
}else{
//判断是不是完全树
LL deepth=;//树的深度(不包括最后一行,根节点深度为0)
LL cur=;
bool flag=false;
num[]=;
while(num[deepth]<n){
deepth++;
cur*=k;
num[deepth]=num[deepth-]+cur;//计算出根节点到每层的节点数
if(num[deepth]==n){
flag=true;
break;
}
}
if(flag==true){//如果是完全树
if(k%==){//偶数叉树,异或到最后只是一个根节点了
printf("%lld\n",n);
}else{//奇数叉树,异或到最后每层剩一个节点
cur=;
while(cur<n){
res^=cur;
cur*=k;
}
printf("%lld\n",res);
}
}else{//如果不是完全树
// 整棵树
res = n; // 最底层单独做
res ^= (n - num[deepth-]) & ; --deepth;
LL p = (n - ) / k; // [(n - 1) - 1] / k,倒数第 2 层开始
LL lid, rid, lnum, rnum, lch;
for(LL d = ; p > ; p = (p - ) / k, ++d, --deepth)
{
// 当前层最左边结点的标号
lid = num[deepth-]; // 当前层最右边结点的标号
rid = num[deepth] - ; // 左边的子树(满的)的大小
lnum = num[d]; // 右边的子树(少一层,但也是满的)的大小
rnum = num[d - ]; if((p - lid) & )
res ^= lnum; if((rid - p) & )
res ^= rnum; lch = p; // p 为根的子数最左小角的后代结点 while(lch <=(n - ) / k) // lch * k + 1 <= n - 1
lch = lch * k + ; res ^= num[d-] + n - lch;
}
printf("%lld\n",res);
}
}
}
}
return ;
}

HDU 6121 Build a tree(找规律+模拟)的更多相关文章

  1. HDU 6121 Build a tree(完全K叉树)

    http://acm.hdu.edu.cn/showproblem.php?pid=6121 题意:给你一颗完全K叉树,求出每棵子树的节点个数的异或和. 思路: 首先需要了解一些关于完全K叉树或满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(k叉树的子树大小相异)

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

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

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

  6. hdu 6121 Build a tree

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

  7. HDU 3032 multi-sg 打表找规律

    普通NIM规则加上一条可以分解为两堆,标准的Multi-SG游戏 一般Multi-SG就是根据拓扑图计算SG函数,这题打表后还能发现规律 sg(1)=1 sg(2)=2 sg(3)=mex{0,1,2 ...

  8. 洛谷 P1014 Cantor表【蛇皮矩阵/找规律/模拟】

    题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … ...

  9. hdu 1030 Delta-wave(数学题+找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1030 Delta-wave Time Limit: 2000/1000 MS (Java/Others ...

随机推荐

  1. 如何面试 PHP 工程师?

    1,解决问题的能力和掌握的知识,看你招聘的目的而决定其二者的平衡.了解流体力学的确会对通下水道有很大帮助,但流体力学专家未必都会疏通下水道. 2,创造力,一个没有自己作品的程序员不是好程序员.编程跟写 ...

  2. AngularJS指南文档

    点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 核心概念 模板 在Angular应用当中,我们的工作就是将服务器的数据填充到客户端页面模 ...

  3. 关于如何更好地使用Github的一些建议

    关于如何更好地使用Github的一些建议 原文(Github repository形式): https://github.com/Wasdns/github-example-repo 本文记录了我对于 ...

  4. python采用 多进程/多线程/协程 写爬虫以及性能对比,牛逼的分分钟就将一个网站爬下来!

    首先我们来了解下python中的进程,线程以及协程! 从计算机硬件角度: 计算机的核心是CPU,承担了所有的计算任务.一个CPU,在一个时间切片里只能运行一个程序. 从操作系统的角度: 进程和线程,都 ...

  5. Ubuntu 16安装GPU版本tensorflow

    pre { direction: ltr; color: rgb(0, 0, 0) } pre.western { font-family: "Liberation Mono", ...

  6. cors解决ajax请求跨域问题

    Access-Control-Allow-Origin: * 适用tomcat部署的项目 在web.xml里添加以下内容 <filter> <filter-name>CorsF ...

  7. 用git从github网站上下载代码的方式

    原本单击如下下载按钮即可 但有时候github异常,该按钮无效,可以使用如下方法: 1.复制url,如https://github.com/ulli-kroll/mt7610u 2.进入要存放该代码的 ...

  8. ubuntu中运行python脚本

    1. 运行方式一 新建test.py文件: touch test.py 然后vim test.py打开并编辑: print 'Hello World' 打开终端,输入命令: python test.p ...

  9. Java web JavaScript DOM 编程

     JavaScript DOM 编程 (1).DOM概述及分类 (2).DOM结构模型:XML DOM 和 HTML DOM 关系? (3).结点,结点树,结点属性与方法? 1.DOM是什么? d ...

  10. 【爬虫入门01】我第一只由Reuests和BeautifulSoup4供养的Spider

    [爬虫入门01]我第一只由Reuests和BeautifulSoup4供养的Spider 广东职业技术学院  欧浩源 1.引言  网络爬虫可以完成传统搜索引擎不能做的事情,利用爬虫程序在网络上取得数据 ...