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. HDU2688-Rotate

    Recently yifenfei face such a problem that give you millions of positive integers,tell how many pair ...

  2. 研磨SpringCloud系列(一)第一个Spring Boot应用

    在此之前,给大家推荐几个东西. STS,Spring官方基于eclipse做的扩展ide.Spring官方背书. 第二个,lombok,注解生成get/set,构造以及基本方法的插件,"隐藏 ...

  3. foreach循环中为什么不要进行remove/add操作

    先来看一段代码,摘自阿里巴巴的java开发手册 List<String> a = new ArrayList<String>(); a.add("1"); ...

  4. Raspiberry Camera详解+picamera库+Opencv控制

    使用树莓派的摄像头,将树莓派自身提供的picamera的API数据转换为Python Oencv可用图像数据: # import the necessary packages from picamer ...

  5. Springboot - 学习笔记 ②

    前言 这一篇是关于spring boot中的配置(configuration)的介绍,我们接下来要说的男主就是 “application.properties”. “男神”默认是生成在“/src/ma ...

  6. asp.net或javascript判断是否手机访问

    /// <summary> /// 判断手机用户UserAgent /// </summary> /// <returns></returns> pri ...

  7. Sqlite常用sql语句

    sqlite常用sql语句 --返回UTC时间 select CURRENT_TIMESTAMP; --返回本地时间 select datetime(CURRENT_TIMESTAMP,'localt ...

  8. doubi -- 初创

    doubi -- 初创 [背景分析] 一直在苦苦探寻人生的价值和意义.在这"二八"年华,对IT工作有点厌倦了.每天都是无休止地问题定位,需求会议.碎片化的时间写出来的代码都是无比的 ...

  9. Python和SQL Server 2017的强大功能

    Python和SQL Server 2017的强大功能 摘要: 源:https://www.red-gate.com/simple-talk/sql/sql-development/power-pyt ...

  10. JVM性能调优,GC

    刚刚做完了一个项目的性能测试,“有幸”也遇到了内存泄露的案例,所以在此和大家分享一下. 主要从以下几部分来说明,关于内存和内存泄露.溢出的概念,区分内存泄露和内存溢出:内存的区域划分,了解GC回收机制 ...