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. day12<常见对象+>

    常见对象(Scanner的概述和方法介绍) 常见对象(Scanner获取数据出现的小问题及解决方案) 常见对象(String类的概述) 常见对象(String类的构造方法) 常见对象(String类的 ...

  2. 深入理解计算机系统chapter1

    ---恢复内容开始--- 预处理器+编译器+汇编器+链接器=编译系统 运行hello程序 操作系统: 无论是在单核还是多核系统中,一个CPU看上去都在并发的执行多个进程,这是通过处理器在进程间切换来实 ...

  3. PHP浮点型(float)转换为整形(int)/round()保留小数点后几位

    round(x,y); x:需要转换的变量 y:保留几位小数 <?php echo round(3.112312321) //输出3 echo round(3.112312321,3) //输出 ...

  4. node-Telnet

    什么是Telnet(window系统) 使用Telnet工具作为客户端对创建的TCP服务器进行会话交流时遇到的问题做一个简单的记录.希望可以帮助到一些同学. 这里看一下百度词条的解释 Telnet协议 ...

  5. springMVC中的redirect和forward区别?

    1.forward在跳转后可以取到message值,redirect在跳转后无法取到message值. 2.forward跳转后地址栏URL不会改变,而redirect会改变.

  6. HDU1421搬寝室(简单DP)

    当然,还可以加滚动数组优化. #include<cstdio> #include<cstdlib> #include<iostream> #include<m ...

  7. HTML与标记属性

    网站部分:UI:AI.PS 前端:html.css.js 网站:是一个存放在网络服务器上的完整信息的集合体.由域名.空间服务器.网站程序.数据库等组成.由多个网页以一定的方式连接在一起,成为一个整体. ...

  8. Entity Framework 之Code First自动数据迁移

    using MvcShopping.Migrations; using MvcShopping.Models; using System; using System.Collections.Gener ...

  9. spring两大核心对象IOC和AOP(新手理解)

    现在自己对spring的理解是:spring的主要的作用是用来进行业务的处理和实现类与类之间的解耦. 其中实现解耦功能 是IOC(控制反转)通过sessionfactory工厂来为需要的对象注入对应的 ...

  10. Appium-desktop安装与使用

    Appium-desktop是什么? 项目描述: Appium Server and Inspector in Desktop GUIs for Mac, Windows, and Linux. Ap ...