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. C++中const几中用法

    转载自:http://www.cnblogs.com/lichkingct/archive/2009/04/21/1440848.html 1. const修饰普通变量和指针 const修饰变量,一般 ...

  2. python进阶之Socket 网络编程

     一:网络编程介绍   自从互联网诞生以来,现在基本上所有的程序都是网络程序,很少有单机版的程序了. 计算机网络就是把各个计算机连接到一起,让网络中的计算机可以互相通信.网络编程就是如何在程序中实现两 ...

  3. 初次就这么给了你(Django-rest-framework)

    Django-Rest-Framework Django-Rest框架是构建Web API强大而灵活的工具包. 简单粗暴,直奔主题. pip install django pip install dj ...

  4. Invoke 用法

    转自:http://blog.sina.com.cn/s/blog_5a6f39cf0100s23x.html 在多线程编程中,我们经常要在工作线程中去更新界面显示,而在多线程中直接调用界面控件的方法 ...

  5. Eclipse中添加文档注释快捷键

    该博客仅记录自己添加文档注释时的操作,由于参考文档地址忘了,因此如果与其他文档重复,请见谅 以下是我的操作过程: 例如: /**   * @param     * @return   */ 快捷键为: ...

  6. 1.在CentOS 6.4安装python3

    CentOS安装Python3.X 1.系统环境说明 [root@Python ~]# uname -r 2.6.32-431.el6.i686 [root@Python ~]# uname -m i ...

  7. 如何用Python脚本从文件读取数据?

    最近自学Python的进度比较慢,工作之余断断续续的看着效率比较低,看来还是要狠下心来每天进步一点点. 还记得前段时间陈大猫提了一口"先实现用python读取本地文件",碰巧今天看 ...

  8. Quartz源码——Quartz调度器的Misfire处理规则(四)

    Quartz调度器的Misfire处理规则 调度器的启动和恢复中使用的misfire机制,还需细化! SimpleTrigger的misfire机制 默认的 Trigger.MISFIRE_INSTR ...

  9. maven编译时错误:无效的目标发行版

    (转)Maven 将依赖打进一个jar包 博客分类: maven   maven配置 <?xml version="1.0" encoding="UTF-8&quo ...

  10. NOIP2017SummerTraining0710

    个人感受:这套题,题目泄露,没什么好打的,第一题刚开始题目理解错误,后来还行,第二道题,打了一个50还是60分的dp,第三道暴力过了小数据,拿了200分,排名15+. 问题 A: 七天使的通讯 时间限 ...