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. Linux Ubuntu从零开始部署web环境及项目 -----快捷键设置(四)

    上篇将了如何在linux部署web项目,这篇介绍如何设置常用快捷键 一.路径快捷键设置 临时快捷键设置:  执行XShel,输入: alias 'aa=cd /etc/sysconfig'       ...

  2. Python里Pure paths、PurePosixPath、PureWindowsPath的区别

    Python是跨平台的,可以在不同的操作系统上运行.但是不同系统上路径 的表示方式是不一样的. 例如windows上路径使用“\”分割子目录和父目录,linux上是使用“/”来分割.这就是PurePo ...

  3. MyBatis注解select in参数

    /** *  * @param ids '1,2,3' * @return */ @Select("select * from user_info where id in (${ids})& ...

  4. Node.js之循环依赖

    在Node.js中有可能会出现循环依赖的问题,在此做一个简单的记录 假如有一个模块A: exports.loaded = false; const b = require('./b'); module ...

  5. Crossin-8-1;8-2课程记录

    打开文件:    open,注意打开文件的路径    读取结束需使用close读取文件:    read    readlines    readline    for in 重置光标位置:   se ...

  6. Python自学笔记-Django分页器小实例

    from django.core.paginator import Paginator iter = 'abcdefhijklmnopqw' paginator = Paginator(iter,4) ...

  7. JavaScript性能优化之函数节流(throttle)与函数去抖(debounce)

    函数节流,简单地讲,就是让一个函数无法在很短的时间间隔内连续调用,只有当上一次函数执行后过了你规定的时间间隔,才能进行下一次该函数的调用. 函数节流的原理挺简单的,估计大家都想到了,那就是定时器.当我 ...

  8. ZOJ 2042 Divisibility (DP)

    Divisibility Time Limit: 2 Seconds      Memory Limit:65536 KB Consider an arbitrary sequence of inte ...

  9. Oracle的常用命令之备份和恢复数据库

    1 将数据库TES完全导出,用户名system 密码manager 导出到D:\daochu.dmp中 exp system/manager@TEST file=d:\daochu.dmp 2 将数据 ...

  10. SerialPort如何读取串口数据并显示在TextBox上,多线程委托

    namespace SerialPort { public partial class Form3 : Form { delegate void UpdateTextEventHandler(stri ...